gpt4 book ai didi

arrays - 使用rust "the trait ` std::array::LengthAtMost3 2` is not implemented"

转载 作者:行者123 更新时间:2023-12-03 11:25:45 25 4
gpt4 key购买 nike

这是怎么回事,

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 上的文档

This limitation on the size N exists because Rust does not yet support code that is generic over the size of an array type. [Foo; 3] and [Bar; 3] are instances of same generic type [T; 3], but [Foo; 3] and [Foo; 5] are entirely different types. As a stopgap, trait implementations are statically generated up to size 32.

这回答了我的两个问题,

  • Rust 缺少数组类型大小的泛型,
  • 32 只是一个可接受的折衷方案,没有强有力的技术理由。似乎这个特征被这样命名是因为它是 applied with macro.

来自this question ,对此没有可接受的解决方法。这只是一个硬限制。

关于arrays - 使用rust "the trait ` std::array::LengthAtMost3 2` is not implemented",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61536058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com