Rust稳定的标准库是否使用不稳定的功能



在Rust标准库中,您可以看到这样的实现,它们使用const泛型:

#[stable(feature = "vec_from_array", since = "1.44.0")]
impl<T, const N: usize> From<[T; N]> for Vec<T> {
#[cfg(not(test))]
fn from(s: [T; N]) -> Vec<T> {
<[T]>::into_vec(box s)
}
#[cfg(test)]
fn from(s: [T; N]) -> Vec<T> {
crate::slice::into_vec(box s)
}
}

当我试图在我的代码中做同样的事情时

impl<const N: usize> From<[u8; N]> for Binary {
fn from(source: [u8; N]) -> Self {
// Implementation available for $N <= 32.
// Requires https://caniuse.rs/features/vec_from_array, avaiable since Rust 1.44.0.
Self(source.into())
}
}

我得到错误

--> packages/std/src/binary.rs:105:12
|
105 | impl<const N: usize> From<[u8; N]> for Binary {
|            ^
|
= note: see issue #74878 <https://github.com/rust-lang/rust/issues/74878> for more information

我使用的是Rust 1.47.0。

这是否意味着Rust稳定的标准库是用不稳定的特性编译的?

是的,它使用了不稳定的特性。--彼得·霍尔

对于Rust 1.47.0的liballoc(其中定义了Vec(,它使用了大约60个不稳定的特性。

另请参阅:

  • 有没有办法使用Rust-stable中的不稳定模块
  • 有什么方法可以在稳定版或测试版的编译器版本上获得不稳定的功能吗
  • 夜间可用功能的最新列表
  • 板条箱属性是什么?我该把它添加到哪里
  • 在生产中每晚使用Rust
  • 是否可以使用夜间编译器编译一个特定的库,并将其链接到在stable上编译的项目

最新更新