Arrays “生锈”;特性'std::array::lengthatmos32'未实现";

Arrays “生锈”;特性'std::array::lengthatmos32'未实现";,arrays,rust,traits,display,Arrays,Rust,Traits,Display,这是怎么回事 let a = Box::new([2; 10]); println!( "foo {:?}", a ); 但事实并非如此 let a = Box::new([2; 100]); println!( "foo {:?}", a ); 为什么数组的长度决定trait实现?这个错误似乎与问题无关。相反,如何用上面的代码实现我想要的效果 错误再现为: error[E0277]: arrays only have std trait implementations for length

这是怎么回事

let a = Box::new([2; 10]);
println!( "foo {:?}", a );
但事实并非如此

let a = Box::new([2; 100]);
println!( "foo {:?}", a );
为什么数组的长度决定trait实现?这个错误似乎与问题无关。相反,如何用上面的代码实现我想要的效果

错误再现为:

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`.
error[E0277]:数组只有长度为0..=32的std-trait实现
-->/测试rs:4:27
|
4 | println!(“foo{:?}”,a);
|^trait`std::array::lengthatmost22`未为“[{integer};100]实现`
|
=注意:由于对“[{integer};100]的`std::fmt::Debug` impl的要求,因此需要此选项`
=注意:由于对`std::boxed::Box`的`std::fmt::Debug` impl的要求,因此需要此选项`
=注意:std::fmt::Debug::fmt需要`
错误:由于上一个错误而中止
有关此错误的详细信息,请尝试“rustc--explain E0277”。
来自上的文档

对大小N存在此限制是因为Rust尚不支持超过数组类型大小的泛型代码。[Foo;3]和[Bar;3]是相同泛型类型[T;3]的实例,但[Foo;3]和[Foo;5]是完全不同的类型作为权宜之计,trait实现是静态生成的,最大大小为32。

这回答了我的两个问题

  • Rust缺少数组类型大小的泛型
  • 32是一个被接受的折衷方案,没有强有力的技术理由。似乎这种特质之所以如此命名是因为它是

从目前来看,没有可接受的解决方法。这只是一个硬限制。

这能回答你的问题吗?不是真的,我在这个问题上问为什么。为什么这是一个约束?为什么选择长度=32?我想理解这一点,不只是被告知这是不可能的。啊,好吧。这里有一点关于为什么一些N被选择在。除了“大多数时候它看起来足够大”之外,我不确定为什么选择32还有更深层次的原因。@Ryan1729谢谢,这正是我想要的!