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

Borrow to read and change

A function can inspect or update a label without taking ownership. Its signature shows which permission it requests.

Learning paths12 min

Let's discover together

  1. 1

    A reference & accesses a value without taking ownership. A function that only reads text can accept &str; &name converts the String borrow when needed.

  2. 2

    &mut String permits changing that String. Both the original variable and the borrow must permit mutation. text.push_str needs no explicit * because method calls resolve the reference.

  3. 3

    Shared references to the same value can coexist; a mutable borrow needs exclusive access while in use. Scenario 2 keeps a read reference in use until after the change.

  4. 4

    In many cases the compiler ends a borrow after its last use, before the closing brace. Separating reading and writing into stages helps more than adding clone without understanding the conflict.

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 length(text: &str) -> usize { text.len() }
fn mark(text: &mut String) { text.push_str("!"); }
fn main() {
    let mut name = String::from("Tico");
    println!("{}", length(&name));
    mark(&mut name);
    println!("{}", length(&name));
}

Official links and next step

✦
YOUR MISSION

Use &str and &mut String and recognise conflicting borrows.

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 length(text: &str) -> usize { text.len() }
fn mark(text: &mut String) { text.push_str("!"); }
fn main() {
    let mut name = String::from("Tico");
    println!("{}", length(&name));
    mark(&mut name);
    println!("{}", length(&name));
}
Build your answers and check when you are ready.
A moment to understand

Which contract permits changing the borrowed String?

I need a hint

&mut communicates exclusive mutable access while the borrow is in use.

Loading your journey…

Take it with you

Move println!("{view}") before name.push_str in scenario 2. Compile and explain why use order matters. Write a function that appends two signs and check the length.

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.