Flash cards
Review the key moves
What is the main idea behind Rust Ownership?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
___ a = String::from("Hello");Put the learning moves in the order that makes the concept easiest to apply.
Ownership
Rust uses "ownership" to manage memory in a safe way.
Every value in Rust has an owner . The owner is usually a variable.
Ownership Rules
- Each value has one owner
- When the owner goes out of scope, the value is deleted
- You can only have one owner at a time, unless you borrow it (covered in the next chapter)
Basic Ownership Example
In this example, a owns the string. Then we move it to b :
Example
let a = String::from("Hello");
let b = a;
// println!("{}", a); Error: a no longer owns the value
println!("{}", b); // Ok: b now owns the valueWhen we assign a to b , the ownership moves . This means only b can use the value now, because a is no longer valid.
But simple types like numbers, characters and booleans are copied , not moved.
This means you can still use the original variable after assigning it to another:
Example
let a = 5;
let b = a;
println!("a = {}", a); // Works
println!("b = {}", b); // WorksHere, a is copied into b , not moved, so you can still use b .
Clone
For other types, like String , if you really want to keep the original value and also assign it to another variable, you can use the .clone() method, which makes a copy of the data:
Example
let a = String::from("Hello");
let b = a.clone(); // Now both have the same
value
println!("a = {}", a); // Works
println!("b = {}", b);
// WorksHowever, if you don't need to own the value twice, using a reference ( & ) is usually better than cloning, which you will learn more about in the next chapter.
Why Ownership Matters
- Rust uses ownership to automatically free memory when it's no longer needed
- It prevents bugs like using memory that's already been deleted
- It is one of the reasons Rust is so safe and fast