OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Ownership, moves and explicit clones

Explain why moving a String prevents use through its previous binding.

Lesson 10 of 30 · About 25 minutes · Beginner · English

By the end of this lesson

Explain why moving a String prevents use through its previous binding.

Before you start

  • Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
  • Complete lesson 9, or be comfortable explaining and running its examples.

Three words to know

Owner
The binding responsible for dropping a value.
Move
Transfer of ownership without an implicit deep copy.
Clone
An explicit request to duplicate an owned value.

1. Understand the idea

Owned values are dropped when their owner leaves scope. Assigning a String to another binding moves its ownership; the first binding can no longer be used. This is checked before the program runs. A clone copies the owned string data and makes an independent value. Cloning can be appropriate, but do not add it automatically to silence every ownership error.

A String moves from one binding to another; a shared borrow reads without taking ownership.
Move transfers the owner. Borrowing leaves ownership with the caller.

2. Save, run and observe

Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.

Make the copy explicitRUST · save as lesson.rs
fn main() {
    let original = String::from("open");
    let copy = original.clone();
    println!("{original} {copy}");
}
What you should see · example output
open open
Expected compiler rejectionRUST · save as move_error.rs
fn main() {
    let original = String::from("open");
    let moved = original;
    println!("{original} {moved}");
}
What you should see · example output
Compilation fails: use of moved value original. Exact diagnostic layout varies.
Compile and run on macOS/LinuxTERMINAL · Bash or Windows PowerShell
rustc --edition=2024 lesson.rs -o lesson
./lesson

3. Explain the result

The successful example has two owned strings. In move_error.rs, moved takes ownership and original is invalid for later use. Save that second example separately and compile it to inspect E0382; it is intentionally not runnable. Small Copy values such as integers behave differently from String.

YOUR TURN

A small check before you continue

If a helper only needs to inspect a string, must it own a clone?

Show a suggested answer

No. It can usually borrow &str. Borrowing is the next lesson and avoids an unnecessary allocation.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.