Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 879
Nikolai Tschacher Avatar asked Oct 09 '15 11:10

Nikolai Tschacher


People also ask

Can you print an array in Java?

We cannot print array elements directly in Java, you need to use Arrays. toString() or Arrays. deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.


1 Answers

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.

like image 101
Lukas Kalbertodt Avatar answered Oct 04 '22 16:10

Lukas Kalbertodt