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

Array of structs

  • Debug

  • dead_code

  • We can also create an array from structs.

#[derive(Debug)]
#[allow(dead_code)]
struct Person {
    fname: String,
    lname: String,
}

fn main() {
    let mut people = [
        Person {
            fname: String::from("John"),
            lname: String::from("Lennon"),
        },
        Person {
            fname: String::from("Paul"),
            lname: String::from("McCartney"),
        },
    ];

    println!("{:#?}", people);

    people[0].fname = String::from("Jane");
    println!("{:#?}", people);
}
[
    Person {
        fname: "John",
        lname: "Lennon",
    },
    Person {
        fname: "Paul",
        lname: "McCartney",
    },
]
[
    Person {
        fname: "Jane",
        lname: "Lennon",
    },
    Person {
        fname: "Paul",
        lname: "McCartney",
    },
]