How to print structs and arrays?

Rust

Rust Problem Overview


Go seems to be able to print structs and arrays directly.

struct MyStruct {
    a: i32,
    b: i32
}

and

let arr: [i32; 10] = [1; 10];

Rust Solutions


Solution 1 - Rust

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);
}

Solution 2 - Rust

As mdup says, you can use Debug, but you can also use the Display trait.

All types can derive (automatically create) the fmt::Debug implementation as #[derive(Debug)], but fmt::Display must be manually implemented.

You can create a custom output:

struct MyStruct {
    a: i32,
    b: i32
}

impl std::fmt::Display for MyStruct {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "(value a: {}, value b: {})", self.a, self.b)
    }
}

fn main() {
    let test = MyStruct { a: 0, b: 0 };

    println!("Used Display: {}", test);    
}

Shell:

Used Display: (value a: 0, value b: 0)

For more information, you can look at the fmt module documentation.

Solution 3 - Rust

As no one here explicitly answers for arrays, to print out an array you need to specify the {:?}, also used to print debug output

let val = 3;
let length = 32; // the maximum that can be printed without error
let array1d = [val; length];
let array2d = [array1d; length]; // or [[3; 32]; 32];
let array3d = [array2d; length]; // or [[[3; 32]; 32]; 32];

However arrays where length > 32 will exit with an error:

let length = 33;
let array1d = [3; length];
println("{:?}", array1d);

error[E0277]: the trait bound `[{integer}; 33]: std::fmt::Debug` is not satisfied
--> src\main.rs:6:22
|
|     println!("{:?}", array1d);
|                      ^^^^^^^ the trait `std::fmt::Debug` is not implemented for `[{integer}; 33]`

Longer arrays can be printed out with the approach from this answer: https://stackoverflow.com/questions/30901965/implement-debug-trait-for-large-array-type

Solution 4 - Rust

Actually just {:?} is sufficient.

let a = [1, 2, 3, 4, 5];
let complete = &a[..];
println! ("{:?}", a);
println! ("{:?}", complete);

Solution 5 - Rust

#[derive(Debug)]
 struct Rectangle{
       width: u32,
       height: u32,
  }

fn main(){
   let rec = Rectangle{
      width: 50,
      height: 30,
   };

   println!("The rectangle {:?} ", rec);
   println!("The area of the rectangle is {} pixels", 
   area_rectangle(&rec));
}

fn area_rectangle(rectangle: &Rectangle) -> u32{
    rectangle.width * rectangle.height
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontezView Question on Stackoverflow
Solution 1 - RustmdupView Answer on Stackoverflow
Solution 2 - RustAngel AngelView Answer on Stackoverflow
Solution 3 - RustdtasevView Answer on Stackoverflow
Solution 4 - RustJenny LianView Answer on Stackoverflow
Solution 5 - RustIan GithungoView Answer on Stackoverflow