Why does println! work only for arrays with a length less than 33?

こ雲淡風輕ζ 提交于 2019-11-27 05:24:03

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!