Ownership helps Rust keep track of who is responsible for data. It prevents mistakes like using data after it is gone or changing data at the same time from different places.
When you want to make sure your program does not crash because of wrong data use.
When you want to avoid confusing bugs caused by multiple parts changing the same data.
When you want your program to be safe and fast without needing extra checks while running.
Syntax
Rust
Ownership is a set of rules:
1. Each value has one owner.
2. When the owner goes out of scope, the value is dropped.
3. You can transfer ownership or borrow the value temporarily.
Ownership rules are checked at compile time, so your program is safe before it runs.
Borrowing lets you use data without taking ownership, which helps avoid mistakes.
Examples
Here, ownership of the string moves from s to t. After that, s cannot be used.
Rust
let s = String::from("hello");
// s owns the string data
let t = s; // ownership moves from s to t// s can no longer be usedprintln!("{}", t);
This example shows borrowing. The function uses s without taking ownership, so s can still be used after.
Rust
let s = String::from("hello");
let len = calculate_length(&s); // borrow s
fn calculate_length(s: &String) -> usize {
s.len()
}
println!("Length is {}", len);
Sample Program
This program shows ownership moving from s1 to s2. It also shows borrowing with s3 to safely get the length without moving ownership.
Rust
fn main() {
let s1 = String::from("hello");
let s2 = s1; // ownership moves to s2// println!("{}", s1); // error: s1 is no longer validprintln!("{}", s2);
let s3 = String::from("world");
let len = calculate_length(&s3); // borrow s3println!("Length of '{}' is {}", s3, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
}
OutputSuccess
Important Notes
Ownership helps Rust avoid memory bugs without needing a garbage collector.
Understanding ownership is key to writing safe and efficient Rust code.
Summary
Ownership means each value has one owner responsible for it.
Ownership rules prevent bugs like using data after it is freed.
Borrowing lets you use data temporarily without taking ownership.
Practice
(1/5)
1. Why does Rust use ownership rules for managing memory?
easy
A. To make the code run slower
B. To allow multiple owners for the same data
C. To prevent bugs like using data after it is freed
D. To avoid writing any functions
Solution
Step 1: Understand ownership purpose
Ownership ensures each value has a single owner responsible for its memory.
Step 2: Identify why ownership is needed
This prevents bugs like using data after it is freed, which can cause crashes or security issues.
Final Answer:
To prevent bugs like using data after it is freed -> Option C
Quick Check:
Ownership prevents use-after-free bugs = B [OK]
Hint: Ownership prevents memory bugs by single ownership [OK]
Common Mistakes:
Thinking ownership slows code down
Believing multiple owners are allowed
Confusing ownership with function writing
2. Which of the following is the correct way to declare ownership of a String in Rust?
easy
A. let s = &mut "hello";
B. let s: &str = "hello";
C. let s = &String::from("hello");
D. let s = String::from("hello");
Solution
Step 1: Identify ownership declaration
Using let s = String::from("hello"); creates a String with ownership.
Step 2: Check other options
let s: &str = "hello"; is a string slice, not owned String. let s = &String::from("hello"); is a reference, not ownership. let s = &mut "hello"; is invalid syntax.
Final Answer:
let s = String::from("hello"); -> Option D
Quick Check:
String::from creates owned String = A [OK]
Hint: Use String::from to create owned String [OK]
Common Mistakes:
Confusing &str with owned String
Using references instead of ownership
Incorrect mutable reference syntax
3. What will happen when this Rust code runs?
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
medium
A. Compilation error due to use of moved value
B. Prints 'hello' twice
C. Prints 'hello' once, then error at runtime
D. Prints nothing
Solution
Step 1: Understand move semantics
Assigning s2 = s1 moves ownership from s1 to s2. s1 is no longer valid.
Step 2: Check usage of moved value
Using s1 after move in println! causes compile-time error.
Final Answer:
Compilation error due to use of moved value -> Option A
Quick Check:
Using moved value causes compile error = A [OK]
Hint: Ownership moves on assignment; old owner invalid [OK]
Common Mistakes:
Expecting both variables to own data
Thinking code runs but errors at runtime
Confusing move with copy
4. Find the error in this Rust code related to ownership:
fn main() {
let s = String::from("hello");
takes_ownership(s);
println!("{}", s);
}
fn takes_ownership(some_string: String) {
println!("{}", some_string);
}
medium
A. Cannot use s after it was moved to takes_ownership
B. Function takes_ownership should return String
C. String::from is incorrect syntax
D. println! macro is used incorrectly
Solution
Step 1: Analyze ownership transfer
Passing s to takes_ownership moves ownership to the function parameter.
Step 2: Identify use after move
Using s in println! after move causes compile error.
Final Answer:
Cannot use s after it was moved to takes_ownership -> Option A
Quick Check:
Use after move causes error = D [OK]
Hint: Ownership moves into function, original variable invalid after [OK]
Common Mistakes:
Thinking function must return ownership
Believing String::from syntax is wrong
Assuming println! usage is incorrect
5. You want to let a function use a String temporarily without taking ownership. Which Rust feature helps achieve this safely?
hard
A. Using global variables
B. Borrowing with references (&String)
C. Cloning the String inside the function
D. Passing the String by value
Solution
Step 1: Understand temporary use without ownership
Borrowing allows a function to use data without taking ownership by passing a reference.
Step 2: Identify correct Rust feature
Using &String passes a reference, so ownership stays with caller.
Final Answer:
Borrowing with references (&String) -> Option B
Quick Check:
Borrowing uses references to avoid ownership transfer = C [OK]
Hint: Use & to borrow without ownership transfer [OK]
Common Mistakes:
Thinking cloning is always needed
Using global variables for ownership
Passing by value transfers ownership
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.