Move semantics help Rust manage memory safely and efficiently by transferring ownership of data instead of copying it.
Move semantics in Rust
let x = String::from("hello"); let y = x; // x is moved to y // x can no longer be used here
After moving, the original variable cannot be used because ownership has transferred.
Move happens automatically for types that do not implement the Copy trait.
a to b. Using a after move causes an error.let a = String::from("rust"); let b = a; // a is moved to b // println!("{}", a); // error: a is no longer valid
let x = 5; let y = x; // x is copied because i32 implements Copy println!("{}", x); // works fine
s1 to the function moves ownership. After the call, s1 is invalid.fn take_ownership(s: String) { println!("Got: {}", s); } fn main() { let s1 = String::from("hello"); take_ownership(s1); // s1 can no longer be used here }
This program shows how ownership moves from s1 to s2. Trying to use s1 after move causes an error.
fn main() {
let s1 = String::from("hello");
let s2 = s1; // move ownership from s1 to s2
println!("s2: {}", s2);
// println!("s1: {}", s1); // this would cause a compile error
}Move semantics prevent double free errors by ensuring only one owner at a time.
Types like String and Vec move by default, while simple types like integers copy.
You can clone data explicitly if you want to keep using the original after copying.
Move semantics transfer ownership of data instead of copying it.
After a move, the original variable cannot be used.
This helps Rust manage memory safely and efficiently.
Practice
Solution
Step 1: Understand move semantics
In Rust, moving a value transfers ownership from one variable to another.Step 2: Effect on original variable
After the move, the original variable is invalid and cannot be used to prevent double free errors.Final Answer:
The original variable can no longer be used. -> Option AQuick Check:
Move means original variable unusable [OK]
- Thinking the original variable still holds a copy
- Assuming Rust automatically clones on move
- Confusing move with borrowing
a to b?Solution
Step 1: Identify move syntax
Assigninglet b = a;moves ownership ofatob.Step 2: Check other options
&aborrows,a.clone()copies,*ais invalid for String.Final Answer:
let b = a; -> Option CQuick Check:
Simple assignment moves ownership [OK]
- Using references instead of moving
- Using clone which copies instead of moves
- Dereferencing a String which is invalid
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);Solution
Step 1: Analyze ownership transfer
Assignings2 = s1moves ownership of the String froms1tos2.Step 2: Using moved variable
Trying to prints1after move causes a compile-time error becauses1is invalid.Final Answer:
Compilation error -> Option AQuick Check:
Using moved variable causes compile error [OK]
- Expecting output 'hello' despite move
- Thinking it causes runtime error
- Assuming Rust clones automatically
fn main() {
let v = vec![1, 2, 3];
let v2 = v;
println!("{}", v.len());
}Solution
Step 1: Understand move of vector
Assigningv2 = vmoves ownership of vectorvtov2.Step 2: Using moved variable causes error
Callingv.len()after move is invalid and causes compile error.Final Answer:
Usingv.len()aftervwas moved tov2. -> Option DQuick Check:
Using moved variable causes compile error [OK]
- Thinking vector cannot be moved
- Ignoring ownership rules after move
- Confusing move with borrowing
fn take_ownership(s: String) {
println!("{}", s);
}
fn main() {
let s = String::from("hello");
take_ownership(s);
println!("{}", s);
}What is the best way to fix the code so
main can print s after calling take_ownership?Solution
Step 1: Understand ownership transfer in function
Passingstotake_ownershipmoves ownership, invalidatingsinmain.Step 2: Ways to keep
Changesusabletake_ownershipto take a reference:fn take_ownership(s: &String). borrowssso ownership is not moved.
Clonesbefore passing:take_ownership(s.clone()). clonessso original remains.
Returnsfromtake_ownershipand assign it back. returns ownership back tomain.Step 3: Best fix
All three options correctly fix the problem.Final Answer:
All of the above. -> Option BQuick Check:
Borrow, clone, or return ownership fixes [OK]
- Ignoring ownership transfer in function calls
- Not cloning when needed
- Not returning ownership back
Ready to prove it?
Pass the test and this pattern moves unknown
