RustArena

Step 7 of 7

A Vec of structs

Most real vectors hold structs: students in a grade book, items in an inventory, orders in a queue. Everything from this module works the same way.

struct Student {
    name: String,
    score: u32,
}

fn main() {
    let mut class = Vec::with_capacity(3);
    class.push(Student { name: String::from("Ana"), score: 91 });
    class.push(Student { name: String::from("Bo"), score: 78 });
    class.push(Student { name: String::from("Cy"), score: 85 });

    for s in &mut class {
        s.score = (s.score + 5).min(100);
    }

    class.sort_by(|a, b| b.score.cmp(&a.score));
    for (i, s) in class.iter().enumerate() {
        println!("{}. {} {}", i + 1, s.name, s.score);
    }

    let best = &class[0];
    println!("best: {}", best.name);
}
1. Ana 96
2. Cy 90
3. Bo 83
best: Ana

with_capacity

Vec::with_capacity(n) allocates room for n elements up front. The vector is still empty (len() == 0), but the first n pushes won't need to grow it. When you know roughly how many elements are coming (the number of lines in a file, the size of another collection), it saves allocations. It's a performance hint, never required for correctness.

Common mistakes

Your task

Write fn restock_list(items: &[Item], min_qty: u32) -> Vec<String> that returns the names of the items whose quantity is below min_qty, in alphabetical order. You'll need owned Strings for the result, so push item.name.clone().

Challenge 2

Write fn most_valuable(items: &[Product]) -> Option<String> that returns the name of the product with the largest stock value (qty * price_cents), or None for an empty slice. On a tie, keep the first one.

Tip: let [first, rest @ ..] = items else { return None; }; handles the empty case and gives you a starting "best" in one line.

Challenge 3

Write a grade book program. Each input line is name score. Store the students in a Vec of a Student struct you define, sort them by score (highest first, equal scores in name order), print them numbered, then print the class average with one decimal:

1. ana 91
2. cy 85
3. bo 78
average: 84.7

Previous: dedup, retain, contains and binary_search