如何运作,
let a = Box::new([2; 10]);
println!( "foo {:?}", a );
但这不是,
let a = Box::new([2; 100]);
println!( "foo {:?}", a );
为什么数组的长度决定特征的实现?该错误似乎与问题无关。相反,如何用上述代码达到我想要的效果。
再现错误是:
error[E0277]: arrays only have std trait implementations for lengths 0..=32
--> ./test.rs:4:27
|
4 | println!( "foo {:?}", a );
| ^ the trait `std::array::LengthAtMost32` is not implemented for `[{integer}; 100]`
|
= note: required because of the requirements on the impl of `std::fmt::Debug` for `[{integer}; 100]`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `std::boxed::Box<[{integer}; 100]>`
= note: required by `std::fmt::Debug::fmt`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
摘自array
上的文档
存在对大小N的限制,因为Rust尚不支持在数组类型的大小上通用的代码。 [Foo; 3]和[Bar; 3]是相同泛型[T; 3],但[Foo; 3]和[Foo; 5]是完全不同的类型。 作为权宜之计,特征实现是静态生成的,最大大小为32。
这回答了我的两个问题,
array
并且从applied with macro.开始,对此没有可接受的解决方法。这只是一个硬限制。