小编SRU*_*SRU的帖子

为什么我会收到错误“const fn 中的函数指针不稳定”,但在包装在 newtype 中时它会消失?

这是预期的行为还是编译器错误?

以下代码无法编译。valuesinMyStruct是一个Option因为Vec::new不是 const fn - 但它Option::None是常量(但它仍然不能编译)。

type MyFun = fn(input: u32) -> u32;

struct MyStruct {
    values: Option<Vec<MyFun>>,
}

impl MyStruct {
    pub const fn init() -> Self {
        Self { values: None }
    }
}

fn main() {
    MyStruct::init();
}
Run Code Online (Sandbox Code Playgroud)

操场

error[E0723]: function pointers in const fn are unstable
 --> src/main.rs:9:24
  |
9 |         Self { values: None }
  |                        ^^^^
  |
  = note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for …
Run Code Online (Sandbox Code Playgroud)

constants rust

2
推荐指数
1
解决办法
312
查看次数

标签 统计

constants ×1

rust ×1