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

Errors we can handle

Input may be text, zero or a quantity above our limit. Result lets us respond without pretending everything succeeded.

Learning paths12 min

Let's discover together

  1. 1

    Result<T, E> has two variants: Ok with the result and Err with the error. Here T is u16 and E is a fixed &'static str message. That text lives in the program for its entire execution.

  2. 2

    trim removes edge whitespace. parse::<u16>() attempts conversion to a 16-bit unsigned integer. Text, negative values and numbers outside the type can fail before the business rule.

  3. 3

    map_err(|_| "number") maps the conversion error to a short message. The small function between bars ignores the original error. ? returns that Err to the caller; next lesson follows this passage.

  4. 4

    Even a valid u16 may be invalid for our workshop: we accept only 1 through 20, inclusive. Avoid unwrap on fallible input; choose one response for Ok and another for Err.

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 main() { println!("{:?}", quantity("3")); }

Official links and next step

✦
YOUR MISSION

Separate conversion from validation and return Ok or Err.

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 main() { println!("{:?}", quantity("3")); }
Build your answers and check when you are ready.
A moment to understand

Does successful conversion make input valid for every rule?

I need a hint

Zero fits u16, but the workshop requires a positive quantity.

Loading your journey…

Take it with you

Test 1, 20, 21, -1, empty text and " 3 ". Group outputs into success, number error and range error. Explain why validation is more than conversion.

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.