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

Collections without guessing indexes

A shelf grows and shrinks. Looking up a missing position should be a handled possibility.

Learning paths12 min

Let's discover together

  1. 1

    Vec<T> stores a sequence of same-type elements and can grow with push. The first index is 0; len() reports the element count, not the last index.

  2. 2

    get(index) returns Option<&T>: Some with a reference if the position exists, None otherwise. items[index] can panic when the position does not exist.

  3. 3

    A function that only reads a sequence can accept &[u16], a borrowed slice. It can inspect a Vec or an array without copying every element.

  4. 4

    Some(0) and None differ: a zero value exists; absence means no value was found. Avoid silently substituting zero when the distinction matters.

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 inspect(items: &[u16], index: usize) { println!("{:?}", items.get(index)); }
fn main() {
    let mut items = vec![3];
    items.push(0);
    inspect(&items, 1);
}

Official links and next step

✦
YOUR MISSION

Use Vec, &[T] slices, get and Option with Some/None.

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 inspect(items: &[u16], index: usize) { println!("{:?}", items.get(index)); }
fn main() {
    let mut items = vec![3];
    items.push(0);
    inspect(&items, 1);
}
Build your answers and check when you are ready.
A moment to understand

Why does Some(0) differ from None?

I need a hint

Option represents presence or absence, not just a number.

Loading your journey…

Take it with you

Test indexes 0, 2 and 3 after push(7). Then pass an empty list. Write separate messages for absence and zero quantity.

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.