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

Propagate without hiding

A record combines a name and quantity. Each step can fail; ? stops the path with the relevant error.

Learning paths12 min

Let's discover together

  1. 1

    Our teaching format is name;quantity, such as sensor;3. split_once finds the first separator. ok_or("format") converts its Option to Result: a missing separator is a format error.

  2. 2

    quantity(raw)? continues with the number on Ok. On Err, it immediately returns that error from the current function. It neither fixes the text nor retries the call.

  3. 3

    Here both functions return the same error type, &'static str. ? can also convert compatible errors in other programs; its enclosing function needs a compatible return type.

  4. 4

    This is not a general CSV reader: it has no quoting, escaping or separators inside names. Documenting limits avoids overstating the parser. In the project, every record is validated before the collection is used.

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.

fn quantity(text: &str) -> Result<u16, &'static str> {
    let n = text.trim().parse::<u16>().map_err(|_| "number")?;
    if !(1..=20).contains(&n) { return Err("range"); }
    Ok(n)
}
fn parse_line(line: &str) -> Result<u16, &'static str> {
    let (name, raw) = line.split_once(';').ok_or("format")?;
    if name.trim().is_empty() { return Err("name"); }
    let amount = quantity(raw)?;
    Ok(amount)
}
fn main() { println!("{:?}", parse_line("sensor;3")); }

Official links and next step

✦
YOUR MISSION

Read simple records and explain how ? propagates errors.

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.

fn quantity(text: &str) -> Result<u16, &'static str> {
    let n = text.trim().parse::<u16>().map_err(|_| "number")?;
    if !(1..=20).contains(&n) { return Err("range"); }
    Ok(n)
}
fn parse_line(line: &str) -> Result<u16, &'static str> {
    let (name, raw) = line.split_once(';').ok_or("format")?;
    if name.trim().is_empty() { return Err("name"); }
    let amount = quantity(raw)?;
    Ok(amount)
}
fn main() { println!("{:?}", parse_line("sensor;3")); }
Build your answers and check when you are ready.
A moment to understand

What happens when quantity returns Err?

I need a hint

Notice the early return, not only the final Ok.

Loading your journey…

Take it with you

Add tests for sensor, ;3 and sensor;21. Draw the stages and mark where each input stops. Decide what an extra line should mean before changing the parser.

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.