为什么 println!仅适用于长度小于 33 的数组? [英] Why does println! work only for arrays with a length less than 33?

查看:41
本文介绍了为什么 println!仅适用于长度小于 33 的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rust 中,这是有效的:

fn main() {让 a = [0;32];println!("{:?}", a);}

但这不会:

fn main() {让 a = [0;33];println!("{:?}", a);}

编译错误:

error[E0277]: trait bound `[{integer};33]:std::fmt::Debug` 不满足-->src/main.rs:3:22|3 |println!("{:?}", a);|^ 特性 `std::fmt::Debug` 没有为 `[{integer}; 实现;33]`|= 注意:`[{整数};33]` 不能使用 `:?` 进行格式化;如果它在您的箱子中定义,请添加`#[derive(Debug)]` 或手动实现它= 注意:`std::fmt::Debug::fmt` 需要

我假设 std::fmt::Debug 函数以某种方式检测到最多 32 个元素的类型,但随后放弃了它的检测.或者为什么它不起作用?

解决方案

从 Rust 1.47 开始 (2020-10-08), 这不再正确 现在几乎所有的 trait 都为任意长度的数组实现了.所以您现在可以打印长度为 33 的数组

下面的旧答案供参考.


遗憾的是,Rust 还不支持整数作为泛型参数.因此,为每个数组 [T; 实现一个特征(如 Debug)并不容易.N].目前,标准库使用一个宏来轻松实现长达 32 的所有长度的特征.

要输出数组,您可以通过这种方式轻松将其转换为切片(&[T]):

让 a = [0;33];println!("{:?}", &a[..]);

顺便说一句:通常你可以通过简单的前缀 & 从数组中获取切片,但是 println 参数的工作有点不同,所以你需要添加全范围索引 [..].


未来情况可能会有所改善.RFC 2000:Const 泛型已被接受并主要在编译器中实现.它允许 impl 块在数组的长度上通用.您可以在相应的跟踪问题上跟踪实施和稳定状态.>

In Rust, this works:

fn main() {
    let a = [0; 32];
    println!("{:?}", a);
}

but this doesn't:

fn main() {
    let a = [0; 33];
    println!("{:?}", a);
}

Compile error:

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`

I assume that the std::fmt::Debug function somehow detects types up to a length of 32 elements, but then drops it's detection. Or why doesn't it work?

解决方案

Starting with Rust 1.47 (2020-10-08), this is no longer true! Almost all traits are now implemented for arrays of arbitrary length. So you can now print arrays of length 33!

Old answer below for reference.


Sadly, Rust does not support integers as generic parameters yet. Therefore it's not easy to implement a trait (like Debug) for every array [T; N]. Currently, the standard library uses a macro to easily implement the trait for all length up to 32.

To output the array, you can easily convert it to a slice (&[T]) this way:

let a = [0; 33];
println!("{:?}", &a[..]);

By the way: Normally you can obtain a slice from an array by simply prefixing &, but println arguments work a bit different, so you need to add the full range index [..].


The situation is likely to improve in the future. RFC 2000: Const Generics has already been accepted and mostly implemented in the compiler. It would allow for impl blocks generic over the length of the array. You can track the status of implementation and stabilization on the corresponding tracking issue.

这篇关于为什么 println!仅适用于长度小于 33 的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆