在 Rust 中,这是有效的:
fn main() {
let a = [0; 32];
println!("{:?}", a);
}
但这并没有:
fn main() {
let a = [0; 33];
println!("{:?}", a);
}
编译错误:
error[E0277]: the trait bound `[{integer}; 33]: std::fmt::Debug` is not satisfied
--> src/main.rs:3:22
|
3 | println!("{:?}", a);
| ^ the trait `std::fmt::Debug` is not implemented for `[{integer}; 33]`
|
= note: `[{integer}; 33]` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
= note: required by `std::fmt::Debug::fmt`
我假设std::fmt::Debug
函数以某种方式检测长度最多为 32 个元素的类型,但随后放弃检测。或者为什么它不起作用?
从 Rust 1.47 (2020-10-08) 开始,这不再是真的 https://blog.rust-lang.org/2020/10/08/Rust-1.47.html#traits-on-larger-arrays!几乎所有特征现在都针对任意长度的数组实现。所以现在可以打印长度为 33 的数组!
以下旧答案供参考。
遗憾的是,Rust 还不支持整数作为泛型参数。因此实现一个特质并不容易(比如Debug
) 对于每个数组[T; N]
。目前,标准库使用宏可以轻松实现长度不超过 32 的特征。
要输出数组,您可以轻松地将其转换为切片(&[T]
) 这边走:
let a = [0; 33];
println!("{:?}", &a[..]);
顺便说一下:通常你可以通过简单地添加前缀来从数组中获取切片&
, but println
参数的工作方式有点不同,因此您需要添加全范围索引[..]
.
未来情况可能会有所改善。RFC 2000:常量泛型 https://github.com/rust-lang/rfcs/blob/master/text/2000-const-generics.md已经被接受并大部分在编译器中实现。这将允许impl
在数组的长度上阻止通用。您可以跟踪实施和稳定的状态相应的跟踪问题 https://github.com/rust-lang/rust/issues/44580.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)