0Pricing
Learn Rust Coding · Lesson

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

  1. Creating and Filling Vectors
  2. Indexing and Iterating
  3. Growing and Shrinking
  4. Vectors of Structs
← Back to Learn Rust Coding