snippetrustCritical
How to print structs and arrays?
Viewed 0 times
howarraysandstructsprint
Problem
How do I print structs and arrays in Rust? Other languages are able to print structs and arrays directly.
and
struct MyStruct {
a: i32,
b: i32
}and
let arr: [i32; 10] = [1; 10];Solution
You want to implement the
Debug trait on your struct. Using #[derive(Debug)] is the easiest solution. Then you can print it with {:?}:#[derive(Debug)]
struct MyStruct{
a: i32,
b: i32
}
fn main() {
let x = MyStruct{ a: 10, b: 20 };
println!("{:?}", x);
}Code Snippets
#[derive(Debug)]
struct MyStruct{
a: i32,
b: i32
}
fn main() {
let x = MyStruct{ a: 10, b: 20 };
println!("{:?}", x);
}Context
Stack Overflow Q#30253422, score: 184
Revisions (0)
No revisions yet.