为什么是default :: default()一个常数函数?
从生锈1.6开始,当前特征 default 被定义为,
pub trait Default {
fn default() -> Self;
}
为什么这不是
pub trait Default {
const fn default() -> Self;
}
As of Rust 1.6, the current trait Default
is defined as,
pub trait Default {
fn default() -> Self;
}
Why isn't this though
pub trait Default {
const fn default() -> Self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以实现
默认:: default
不是const
。例如:人为较少的示例将包括引用全局变量,例如某些全局默认配置的
arc
克隆。更改
默认值::默认
即使在Rustc中支持,也是不可接受的变化,并且可以说是不希望的。There are plenty of ways to implement
Default::default
that are notconst
. For example:Less contrived examples would include referencing global variables, such as cloning an
Arc
of some global default configuration.Changing
Default::default
, even if supported in rustc, would be an unacceptably breaking change, and arguably undesired.rustc的硬限制
这是因为目前
是在github#63065 。当可以声明
const
中的功能时,也许可以更好地解决此问题。Hard limitation of rustc
This is because currently,
This is being worked on GitHub #63065. Perhaps there will be a better solution to this problem when functions in traits can be declared
const
.<Foo as Default>::default
in constants"