Vectors of Structs
Hold complex data in a Vec.
Why Vectors of Structs
Vectors can hold any single type, including your own structs. A Vec<Person> stores many people in order, which is great for lists of records like users, items, or scores.
This combines custom data shapes with a growable list.
Defining a Struct
First define the struct you want to store. This Person has a name and an age.
The #[derive(Debug)] line lets us print a person with {:?}, which is useful for whole vectors.
#[derive(Debug)]
struct Person {
name: String,
age: u32,
}All lessons in this course
- Creating and Filling Vectors
- Indexing and Iterating
- Growing and Shrinking
- Vectors of Structs