E0015
一个常量项被非常量表达式初始化。
错误代码示例:
fn create_some() -> Option<u8> {
Some(1)
}
const FOO: Option<u8> = create_some(); // error!
在静态或常量表达式中只能调用 const
函数,还有 struct/enum 构造函数( constructors )。
要修复这个错误,可以将 create_some
声明为常量函数。
const fn create_some() -> Option<u8> { // declared as a const function
Some(1)
}
const FOO: Option<u8> = create_some(); // ok!
// These are also working:
struct Bar {
x: u8,
}
const OTHER_FOO: Option<u8> = Some(1);
const BAR: Bar = Bar {x: 1};
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。