I was trying out the following code and ran into an error.
fn main() {
let a = [12;500];
let b = [15;5];
println!("{:?}",a );
println!("{:?}",b );
}
error[E0277]: the trait bound `[{integer}; 500]: std::fmt::Debug` is not satisfied
--> src\main.rs:37:17
|
37 | println!("{:?}",a );
| ^ `[{integer}; 500]` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
|
= help: the trait `std::fmt::Debug` is not implemented for `[{integer}; 500]`
= note: required by `std::fmt::Debug::fmt`
From https://doc.rust-lang.org/std/primitive.array.html
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.
Could anyone please shed some light on this concept/limitation, I cant wrap my head around this.
So if i wanted to impl Debug and Display traits for a large array is this the right way to do it?
use std::fmt;
struct myStruct {
data:[u8;300]
}
impl myStruct{
pub fn new() -> myStruct{
myStruct {
data:[2;300],
}
}
}
impl fmt::Debug for myStruct {
fn fmt(&self, f:&mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&&self.data[..],f)
}
}
fn main() {
let a = myStruct::new();
println!("{:?}",a );
}