Bird
Raised Fist0
Rustprogramming~20 mins

Ownership rules in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Ownership 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?

Consider the following Rust code snippet. What will it print?

Rust
fn main() {
    let s1 = String::from("hello");
    let s2 = s1;
    println!("{}", s2);
}
Ahello
BCompilation error: use of moved value 's1'
Chellohello
DRuntime error: use of uninitialized variable
Attempts:
2 left
💡 Hint

Remember that in Rust, moving ownership transfers the value, so the original variable cannot be used after move.

❓ Predict Output
intermediate
2:00remaining
What error does this Rust code produce?

Analyze the following Rust code. What error will the compiler report?

Rust
fn main() {
    let s = String::from("hello");
    let r1 = &s;
    let r2 = &s;
    let r3 = &mut s;
    println!("{} {} {}", r1, r2, r3);
}
Ahello hello hello
BCompilation error: cannot borrow `s` as immutable because it is also borrowed as mutable
CCompilation error: cannot borrow `s` as mutable because it is also borrowed as immutable
DRuntime error: simultaneous mutable and immutable borrow
Attempts:
2 left
💡 Hint

Rust enforces that you cannot have mutable and immutable references to the same data at the same time.

🔧 Debug
advanced
2:00remaining
Identify the ownership issue in this Rust function

What is the ownership problem in the following Rust code?

Rust
fn take_ownership(s: String) {
    println!("{}", s);
}

fn main() {
    let s = String::from("hello");
    take_ownership(s);
    println!("{}", s);
}
ACompilation error: use of moved value `s` after it was passed to the function
BRuntime error: double free detected
CCompilation error: missing lifetime specifier
DNo error, prints 'hello' twice
Attempts:
2 left
💡 Hint

Think about what happens to ownership when a variable is passed to a function by value.

📝 Syntax
advanced
2:00remaining
Which option correctly borrows a mutable reference in Rust?

Which of the following code snippets correctly borrows a mutable reference to a variable?

A
let mut x = 5;
let y = &x;
*y += 1;
B
let x = 5;
let y = &mut x;
*y += 1;
C
let mut x = 5;
let y = &mut x;
x += 1;
D
let mut x = 5;
let y = &mut x;
*y += 1;
Attempts:
2 left
💡 Hint

Remember that to borrow a mutable reference, the original variable must be declared mutable.

🚀 Application
expert
2:00remaining
How many items are in the vector after this ownership transfer?

Consider this Rust code. How many elements does the vector v2 contain after the ownership transfer?

Rust
fn main() {
    let v1 = vec![1, 2, 3];
    let v2 = v1;
    // println!("{}", v1.len()); // This line is commented out
    println!("{}", v2.len());
}
A0
B3
CCompilation error: use of moved value `v1`
DRuntime error: vector accessed after move
Attempts:
2 left
💡 Hint

When ownership moves, the new owner has full access to the data.

Practice

(1/5)
1. What happens when ownership of a variable is moved to another variable in Rust?
easy
A. The original variable becomes invalid and cannot be used.
B. Both variables can be used independently without restrictions.
C. The data is copied and both variables own separate copies.
D. The original variable automatically deletes the data.

Solution

  1. Step 1: Understand ownership move

    When ownership moves, the original variable loses its right to access the data.
  2. Step 2: Effect on original variable

    The original variable becomes invalid and cannot be used to prevent double free errors.
  3. Final Answer:

    The original variable becomes invalid and cannot be used. -> Option A
  4. Quick Check:

    Ownership move invalidates old owner = A [OK]
Hint: Ownership move disables old variable usage [OK]
Common Mistakes:
  • Thinking ownership move copies data
  • Assuming both variables remain valid
  • Confusing move with borrowing
2. Which of the following is the correct syntax to borrow a variable immutably in Rust?
easy
A. let ref x = value;
B. let x = value;
C. let x = *value;
D. let x = &value;

Solution

  1. Step 1: Identify borrowing syntax

    Immutable borrowing uses the & symbol before the variable.
  2. Step 2: Match correct syntax

    let x = &value; correctly borrows value immutably.
  3. Final Answer:

    let x = &value; -> Option D
  4. Quick Check:

    Immutable borrow uses & = B [OK]
Hint: Use & to borrow immutably [OK]
Common Mistakes:
  • Using * for borrowing instead of dereferencing
  • Missing & symbol for borrowing
  • Confusing mutable and immutable borrow syntax
3. What will be the output of this Rust code?
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
medium
A. Runtime error
B. hello
C. Compilation error
D. Empty string

Solution

  1. Step 1: Analyze ownership move

    Assigning s1 to s2 moves ownership, invalidating s1.
  2. Step 2: Using invalidated variable

    Printing s1 after move causes a compile-time error because s1 is no longer valid.
  3. Final Answer:

    Compilation error -> Option C
  4. Quick Check:

    Using moved variable causes compile error = A [OK]
Hint: Using moved variable causes compile error [OK]
Common Mistakes:
  • Expecting s1 to print after move
  • Confusing move with copy
  • Thinking runtime error occurs instead
4. Identify the error in this Rust code snippet:
let s = String::from("hello");
let r1 = &s;
let r2 = &mut s;
println!("{} and {}", r1, r2);
medium
A. No error, code runs fine.
B. Cannot borrow s as mutable because it is already borrowed as immutable.
C. String::from is not a valid function.
D. Cannot print two variables in one println! macro.

Solution

  1. Step 1: Check borrowing rules

    Rust forbids mutable borrow if immutable borrow exists simultaneously.
  2. Step 2: Identify conflict

    r1 is an immutable borrow; r2 tries mutable borrow causing error.
  3. Final Answer:

    Cannot borrow s as mutable because it is already borrowed as immutable. -> Option B
  4. Quick Check:

    Mutable borrow conflicts with existing immutable borrow = D [OK]
Hint: Mutable borrow disallowed if immutable borrow exists [OK]
Common Mistakes:
  • Ignoring borrow rules and expecting code to run
  • Thinking multiple mutable borrows allowed
  • Misunderstanding println! macro usage
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 correct way to fix the error without cloning the string?
hard
A. Change the function parameter to borrow the string: fn take_ownership(s: &String)
B. Remove the println! after the function call.
C. Declare s as mutable in main.
D. Use let s = &String::from("hello"); in main.

Solution

  1. Step 1: Understand ownership transfer

    Passing s to take_ownership moves ownership, invalidating s in main.
  2. Step 2: Use borrowing to avoid move

    Changing parameter to &String borrows s, so ownership stays with main.
  3. Step 3: Adjust function call

    Call take_ownership(&s) to pass reference matching new parameter.
  4. Final Answer:

    Change the function parameter to borrow the string: fn take_ownership(s: &String) -> Option A
  5. Quick Check:

    Borrow parameter to keep ownership in caller = C [OK]
Hint: Borrow in function parameter to keep ownership [OK]
Common Mistakes:
  • Trying to use s after move without borrowing
  • Removing code instead of fixing ownership
  • Confusing mutability with ownership
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