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

Who owns the value?

A label moves to another workbench. In Rust, assigning a String can transfer ownership; it does not automatically copy its text.

Learning paths12 min

Let's discover together

  1. 1

    This course builds on Rust I variables, conditions and functions. Review that foundation if needed; Python is not required. Set up Rust/Cargo with your operating system course and check rustc --version and cargo --version.

  2. 2

    String::from creates owned text. let second = first moves that value: second becomes the owner and first can no longer be used. Leaving its scope makes the owner release the resource.

  3. 3

    first.clone() creates another String with independent contents. This has a cost: copy when you need another owner, not just to silence the compiler. Integers such as u32 implement Copy; assigning them keeps both names usable.

  4. 4

    In scenario 1, changing second does not change first. Scenario 2 tries to read first after moving it: compilation fails before execution. Read both the move location and the use location in the diagnostic.

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 main() {
    let first = String::from("Tico");
    let mut second = first.clone();
    second.push_str("!");
    println!("{} {}", first.len(), second.len());
}

Official links and next step

✦
YOUR MISSION

Distinguish move, Copy and clone; interpret a use after move.

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 main() {
    let first = String::from("Tico");
    let mut second = first.clone();
    second.push_str("!");
    println!("{} {}", first.len(), second.len());
}
Build your answers and check when you are ready.
A moment to understand

When is clone a justified choice?

I need a hint

Moving and copying are different operations; clone duplicates String contents.

Loading your journey…

Take it with you

Run scenario 1 in src/main.rs. Then replace first.clone() with first and run cargo check. Explain the error and restore the valid version; record why copying was needed in this example.

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.