Bird
Raised Fist0
Rustprogramming~20 mins

Move semantics in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Move Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code involving move semantics?
Consider the following Rust code. What will it print when run?
Rust
fn main() {
    let s1 = String::from("hello");
    let s2 = s1;
    // println!("{}", s1); // Uncommenting this line causes error
    println!("{}", s2);
}
Ahello
BCompilation error: use of moved value 's1'
Chellohello
DRuntime error
Attempts:
2 left
💡 Hint
Remember that assigning s1 to s2 moves ownership, so s1 cannot be used after.
❓ Predict Output
intermediate
2:00remaining
What happens when you try to use a moved value in Rust?
What error will this Rust code produce?
Rust
fn main() {
    let v1 = vec![1, 2, 3];
    let v2 = v1;
    println!("{}", v1.len());
}
A0
BRuntime panic
CCompilation error: use of moved value 'v1'
D3
Attempts:
2 left
💡 Hint
After moving v1 to v2, v1 cannot be used anymore.
🔧 Debug
advanced
2:00remaining
Identify the cause of the error in this Rust code using move semantics
Why does this Rust code fail to compile?
Rust
fn main() {
    let s = String::from("hello");
    takes_ownership(s);
    println!("{}", s);
}

fn takes_ownership(some_string: String) {
    println!("{}", some_string);
}
As is borrowed but not mutable
Bs is moved into takes_ownership and cannot be used after
Ctakes_ownership does not return s
Ds is copied instead of moved
Attempts:
2 left
💡 Hint
Think about what happens to ownership when passing s to a function.
❓ Predict Output
advanced
2:00remaining
What is the output of this Rust code with cloning and move?
What will this Rust program print?
Rust
fn main() {
    let s1 = String::from("hello");
    let s2 = s1.clone();
    println!("{} {}", s1, s2);
}
Ahello
BCompilation error: use of moved value 's1'
CRuntime error
Dhello hello
Attempts:
2 left
💡 Hint
Cloning creates a deep copy, so both s1 and s2 are valid.
❓ Predict Output
expert
2:00remaining
What is the output of this Rust code involving move and borrowing?
Analyze the code and select the correct output.
Rust
fn main() {
    let s = String::from("hello");
    let r1 = &s;
    let r2 = &s;
    println!("{} and {}", r1, r2);
    let r3 = &mut s;
    println!("{}", r3);
}
ACompilation error: cannot borrow 's' as mutable because it is also borrowed as immutable
Bhello and hello\nhello
Chello and hello
DRuntime panic
Attempts:
2 left
💡 Hint
Rust does not allow mutable and immutable borrows at the same time.

Practice

(1/5)
1. What happens to a variable in Rust after its value is moved to another variable?
easy
A. The original variable can no longer be used.
B. The original variable keeps a copy of the value.
C. The original variable is automatically cloned.
D. The original variable becomes a reference to the new variable.

Solution

  1. Step 1: Understand move semantics

    In Rust, moving a value transfers ownership from one variable to another.
  2. Step 2: Effect on original variable

    After the move, the original variable is invalid and cannot be used to prevent double free errors.
  3. Final Answer:

    The original variable can no longer be used. -> Option A
  4. Quick Check:

    Move means original variable unusable [OK]
Hint: Moving means original variable is invalid after transfer [OK]
Common Mistakes:
  • Thinking the original variable still holds a copy
  • Assuming Rust automatically clones on move
  • Confusing move with borrowing
2. Which of the following Rust code snippets correctly moves ownership of a String from a to b?
easy
A. let b = a.clone();
B. let b = &a;
C. let b = a;
D. let b = *a;

Solution

  1. Step 1: Identify move syntax

    Assigning let b = a; moves ownership of a to b.
  2. Step 2: Check other options

    &a borrows, a.clone() copies, *a is invalid for String.
  3. Final Answer:

    let b = a; -> Option C
  4. Quick Check:

    Simple assignment moves ownership [OK]
Hint: Use simple assignment to move ownership, not references or clone [OK]
Common Mistakes:
  • Using references instead of moving
  • Using clone which copies instead of moves
  • Dereferencing a String which is invalid
3. What is the output of this Rust code?
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
medium
A. Compilation error
B. hello
C. Runtime error
D. Empty string

Solution

  1. Step 1: Analyze ownership transfer

    Assigning s2 = s1 moves ownership of the String from s1 to s2.
  2. Step 2: Using moved variable

    Trying to print s1 after move causes a compile-time error because s1 is invalid.
  3. Final Answer:

    Compilation error -> Option A
  4. Quick Check:

    Using moved variable causes compile error [OK]
Hint: Using moved variable causes compile error, not runtime [OK]
Common Mistakes:
  • Expecting output 'hello' despite move
  • Thinking it causes runtime error
  • Assuming Rust clones automatically
4. Identify the error in this Rust code related to move semantics:
fn main() {
    let v = vec![1, 2, 3];
    let v2 = v;
    println!("{}", v.len());
}
medium
A. Vector v cannot be moved to v2.
B. Cannot print vector length with println!.
C. Missing semicolon after let v2 = v.
D. Using v.len() after v was moved to v2.

Solution

  1. Step 1: Understand move of vector

    Assigning v2 = v moves ownership of vector v to v2.
  2. Step 2: Using moved variable causes error

    Calling v.len() after move is invalid and causes compile error.
  3. Final Answer:

    Using v.len() after v was moved to v2. -> Option D
  4. Quick Check:

    Using moved variable causes compile error [OK]
Hint: After move, original variable cannot be used [OK]
Common Mistakes:
  • Thinking vector cannot be moved
  • Ignoring ownership rules after move
  • Confusing move with borrowing
5. Given this Rust function:
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?
hard
A. Clone s before passing: take_ownership(s.clone()).
B. All of the above.
C. Return s from take_ownership and assign it back.
D. Change take_ownership to take a reference: fn take_ownership(s: &String).

Solution

  1. Step 1: Understand ownership transfer in function

    Passing s to take_ownership moves ownership, invalidating s in main.
  2. Step 2: Ways to keep s usable

    Change take_ownership to take a reference: fn take_ownership(s: &String). borrows s so ownership is not moved.
    Clone s before passing: take_ownership(s.clone()). clones s so original remains.
    Return s from take_ownership and assign it back. returns ownership back to main.
  3. Step 3: Best fix

    All three options correctly fix the problem.
  4. Final Answer:

    All of the above. -> Option B
  5. Quick Check:

    Borrow, clone, or return ownership fixes [OK]
Hint: Borrow, clone, or return ownership to keep variable usable [OK]
Common Mistakes:
  • Ignoring ownership transfer in function calls
  • Not cloning when needed
  • Not returning ownership back
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard