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

A structure with responsibility

In the workshop, equipment has a name and an available quantity. A struct keeps related data together.

Learning paths12 min

Let's discover together

  1. 1

    struct Item defines named fields and their types. This first model stores only quantity: Item { available: 3 } creates an instance. Two instances can hold different values; the final project adds a name and total.

  2. 2

    impl Item groups functions associated with the type. A method with &self reads; one with &mut self can update. Calling item.available() borrows the item for the method.

  3. 3

    take_one checks for a unit before subtracting. When empty, it returns false and preserves the state. This rule prevents subtracting from zero on this path.

  4. 4

    The field available and method available() share a name, but parentheses distinguish the call. The final API will use private fields in a module to concentrate update rules.

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.

struct Item { available: u16 }
impl Item {
    fn available(&self) -> u16 { self.available }
    fn take_one(&mut self) -> bool {
        if self.available == 0 { return false; }
        self.available -= 1;
        true
    }
}
fn main() {
    let mut item = Item { available: 3 };
    item.take_one();
    println!("{}", item.available());
}

Official links and next step

✦
YOUR MISSION

Create structs, implement methods and distinguish &self from &mut self.

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.

struct Item { available: u16 }
impl Item {
    fn available(&self) -> u16 { self.available }
    fn take_one(&mut self) -> bool {
        if self.available == 0 { return false; }
        self.available -= 1;
        true
    }
}
fn main() {
    let mut item = Item { available: 3 };
    item.take_one();
    println!("{}", item.available());
}
Build your answers and check when you are ready.
A moment to understand

Why check before subtracting?

I need a hint

The check protects a specific rule; it does not prove the whole program.

Loading your journey…

Take it with you

Add a second take_one call to scenario 2 and print its returned bool. Explain what the method reads and changes; create another instance without changing the first.

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.