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

Choices that carry data

Checkout and return are different actions, but both need a quantity. An enum expresses these alternatives.

Learning paths10 min

Let's discover together

  1. 1

    enum Action { Take(u16), Return(u16) } defines two variants. Each carries a quantity. Action::Take(2) is more than the number 2: it records checkout intent too.

  2. 2

    match action selects the corresponding arm. In Action::Take(n), n receives that variant’s quantity. The expression result can feed let available.

  3. 3

    match must cover every possibility. Adding a variant requires revisiting matches without a catch-all arm. This helps locate forgotten decisions.

  4. 4

    These examples use previously valid quantities. An enum alone does not guarantee stock or a legitimate return. The final project applies these rules with Result before changing state.

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.

enum Action { Take(u16), Return(u16) }
fn change(stock: u16, action: Action) -> u16 {
    match action { Action::Take(n) => stock - n, Action::Return(n) => stock + n }
}
fn main() { println!("{}", change(5, Action::Take(2))); }

Official links and next step

✦
YOUR MISSION

Represent alternatives with enum and handle them all with match.

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.

enum Action { Take(u16), Return(u16) }
fn change(stock: u16, action: Action) -> u16 {
    match action { Action::Take(n) => stock - n, Action::Return(n) => stock + n }
}
fn main() { println!("{}", change(5, Action::Take(2))); }
Build your answers and check when you are ready.
A moment to understand

What does an exhaustive match guarantee here?

I need a hint

Covering variants and validating quantities are different responsibilities.

Loading your journey…

Take it with you

Add an Inspect variant without a quantity. Predict the diagnostic before compiling; then add an arm that preserves stock.

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.