在 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)
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在我的代码中做同样的事情时
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())
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误
--> 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
Run Code Online (Sandbox Code Playgroud)
我正在使用 Rust 1.47.0。
这是否意味着 Rust 稳定标准库是用不稳定的特性编译的?
是的,它使用不稳定的功能。—彼得·霍尔
对于 Rust 1.47.0 的 liballoc(Vec定义在哪里),它使用了六十个左右的不稳定特性。
也可以看看:
| 归档时间: |
|
| 查看次数: |
294 次 |
| 最近记录: |