Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Printing struct fails

  • We can print the values of the individual attributes of a struct, but we cannot print the whole struct.
struct Animal {
    name: String,
    size: String,
    weight: i32,
}

fn main() {
    let eli = Animal {
        name: String::from("elephant"),
        size: String::from("huge"),
        weight: 100,
    };
    println!("{}", eli.name);
    println!("{}", eli.size);
    println!("{}", eli.weight);

    // println!("{}", eli);
    // `Animal<'_>` doesn't implement `std::fmt::Display`

    // println!("{:?}", eli);
    // `Animal<'_>` doesn't implement `Debug`

    // dbg!(eli);
    // `Animal<'_>` doesn't implement `Debug`
}
elephant
huge
100