Rust“特征`std :: array :: LengthAtMost32`未实现”

问题描述 投票:2回答:1

如何运作,

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`.
arrays rust traits display
1个回答
4
投票

摘自array上的文档

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

这回答了我的两个问题,

  • Rust缺少数组类型大小的泛型,
  • 32只是一个可以接受的折衷,没有充分的技术理由。好像这个特性被这样命名是因为它是array

并且从applied with macro.开始,对此没有可接受的解决方法。这只是一个硬限制。

© www.soinside.com 2019 - 2024. All rights reserved.