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

Modules and boundaries

The workshop grows. Separating stock rules from presentation lets us change one without spreading details of the other.

Learning paths10 min

Let's discover together

  1. 1

    mod inventory { ... } creates a module inside the file. Internal items are private by default from outside the module. pub makes a function accessible through the module path.

  2. 2

    use inventory::capacity lets us write capacity() instead of the full path. use neither copies code nor installs a package. std is the standard library shipped with Rust.

  3. 3

    A public struct can keep private fields. Exposing methods that validate changes protects rules better than allowing arbitrary external assignments. Module privacy organises code; it is not user authentication.

  4. 4

    To split files later, mod inventory; in main.rs looks for inventory.rs or inventory/mod.rs. This course uses an inline module so the published code file remains self-contained.

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.

mod inventory { pub fn capacity() -> u16 { 20 } }
use inventory::capacity;
fn main() { println!("{}", capacity()); }

Official links and next step

✦
YOUR MISSION

Use mod, pub and use to design a small API.

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.

mod inventory { pub fn capacity() -> u16 { 20 } }
use inventory::capacity;
fn main() { println!("{}", capacity()); }
Build your answers and check when you are ready.
A moment to understand

What does use do?

I need a hint

Importing a name and installing a dependency differ.

Loading your journey…

Take it with you

Move the valid module to src/inventory.rs, replace the block with mod inventory; and run cargo check. Record in README that both files are now needed when sharing.

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.