首页 > 解决方案 > 编译错误 len 作为 const fn 还不稳定

问题描述

我的依赖项之一出现错误。这是错误的代码示例:bytes 0.5.2

pub const fn foo(foo: &'static [u8]) -> usize {
    foo.len()
}
error: `core::slice::<impl [T]>::len` is not yet stable as a const fn
 --> <source>:2:5
  |
2 |     foo.len()
  |     ^^^^^^^^^
error: aborting due to previous error
active toolchain
----------------

stable-x86_64-pc-windows-msvc (default) rustc 1.38.0 (625451e37 2019-09-23)

标签: rust

解决方案


0.5.x似乎需要 Rust 1.39,因此最简单的选择可能是升级到最新版本。

错误状态

core::slice::<impl [T]>::len作为 const fn 还不稳定

如果您查看发行说明,1.39您可以看到其中一个条目是

str::len,[T]::len现在str::as_bytes是 const 函数

所以这个箱子特别需要 >=1.39


推荐阅读