Rullst AcademyRullst Academy/Lesson
Rust II: data, choices and confidence · Lesson 07 / 12

A map for each name

Counting equipment by name differs from looking it up by position. A HashMap associates each key with a value.

Learning paths12 min

Let's discover together

  1. 1

    use std::collections::HashMap brings the type name into scope. HashMap<String, u16> relates owned names to quantities. get looks up a key and may return None.

  2. 2

    entry(key).or_insert(0) returns a mutable reference to the existing value or inserts zero if absent. *count += 1 changes the value that reference points to.

  3. 3

    insert on an existing key replaces its value; it does not add automatically. Here entry accumulates deliberately. This small fixed dataset cannot reach the u16 limit.

  4. 4

    HashMap iteration order is not guaranteed. An interface or test needing stable order must sort keys, use BTreeMap or preserve a separate sequence. Here we only look up sensor.

FROM LESSON TO PRACTICE

A guide to follow at your pace

Read the steps before using the reference below. Commands run in the indicated system’s terminal; signup happens on the official site. Rullst Academy exercises are practice and do not verify your installation or external account.

use std::collections::HashMap;
fn main() {
    let mut counts = HashMap::new();
    for name in ["sensor", "sensor", "motor", "sensor"] {
        let count = counts.entry(String::from(name)).or_insert(0_u16);
        *count += 1;
    }
    println!("{}", counts["sensor"]);
}

Official links and next step

✦
YOUR MISSION

Accumulate counts with HashMap without relying on iteration order.

Now it's your turn

Discovery workshop

Read, predict, investigate

Predict before revealing. This panel shows prepared results; it does not compile your code. Compiler errors are summarised; exact wording can vary with the compiler version.
Mini experiment

Learning simulation: compare cards and check your predictions. The browser is not compiling Rust or starting a server.

use std::collections::HashMap;
fn main() {
    let mut counts = HashMap::new();
    for name in ["sensor", "sensor", "motor", "sensor"] {
        let count = counts.entry(String::from(name)).or_insert(0_u16);
        *count += 1;
    }
    println!("{}", counts["sensor"]);
}
Build your answers and check when you are ready.
A moment to understand

How can we obtain a predictably ordered list?

I need a hint

An observed order is not a HashMap contract.

Loading your journey…

Take it with you

Add motor and count it separately. Then replace the accumulator with insert(name, 1), observe the defect and write a test that detects it.

RUST II · .rs

Your Rust II workbench

Read, predict and answer here. To actually compile, extract the activity ZIP into a new folder, open a terminal beside Cargo.toml and use the commands below. Rust and your operating system tools must be installed.

cargo check --offline
cargo test --offline
cargo run --offline
My files and GitHub

The editor below saves a draft in this browser and lets you download or publish your .rs. To run it in the project, save the edited file as src/main.rs. The ZIP contains the starter version; prepared results do not change with your edits. Online grading does not verify local execution.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.