Bird
Raised Fist0
Rustprogramming~20 mins

Box pointer in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Box Pointer Mastery
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 using Box pointer?
Consider the following Rust program that uses a Box pointer. What will it print when run?
Rust
fn main() {
    let b = Box::new(5);
    println!("{}", *b + 10);
}
ACompilation error
B5
C10
D15
Attempts:
2 left
💡 Hint
Remember that Box stores the value on the heap and you need to dereference it to get the value.
❓ Predict Output
intermediate
2:00remaining
What happens when you try to move a Box pointer?
What will be the output or result of this Rust code?
Rust
fn main() {
    let b1 = Box::new(7);
    let b2 = b1;
    println!("{}", *b1);
    println!("{}", *b2);
}
A7
BRuntime panic
CCompilation error due to use of moved value
D0
Attempts:
2 left
💡 Hint
Box pointers have ownership semantics. Moving them invalidates the original variable.
🔧 Debug
advanced
2:00remaining
Why does this Box pointer code cause a compilation error?
Identify the error in this Rust code and what causes it.
Rust
fn main() {
    let b = Box::new(10);
    let r1 = &b;
    let r2 = &mut b;
    println!("{} {}", r1, r2);
}
ACannot borrow `b` as mutable because it is also borrowed as immutable
BCannot borrow `b` as immutable because it is also borrowed as mutable
CBox pointer cannot be borrowed
DNo error, prints '10 10'
Attempts:
2 left
💡 Hint
Rust enforces borrowing rules: you cannot have mutable and immutable borrows at the same time.
❓ Predict Output
advanced
2:00remaining
What is the output of this recursive Box pointer example?
What will this Rust program print?
Rust
enum List {
    Cons(i32, Box<List>),
    Nil,
}

fn main() {
    let list = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
    match list {
        List::Cons(x, box List::Cons(y, box List::Nil)) => println!("{} {}", x, y),
        _ => println!("No match"),
    }
}
ANo match
B1 2
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Pattern matching with Box requires unboxing with `box` keyword.
🧠 Conceptual
expert
2:00remaining
Why use Box pointer in Rust for recursive types?
Which is the main reason to use Box pointer for recursive data types in Rust?
ATo allocate recursive data on the heap and have a known size for the enum
BTo improve runtime speed by avoiding heap allocation
CTo allow multiple ownership of the recursive data
DTo automatically clone the recursive data when copied
Attempts:
2 left
💡 Hint
Rust needs to know the size of types at compile time. Recursive types without indirection have unknown size.

Practice

(1/5)
1. What is the main purpose of a Box pointer in Rust?
easy
A. To create multiple mutable references to the same data
B. To automatically clone data when assigned
C. To store data on the heap and keep a pointer on the stack
D. To replace all references with raw pointers

Solution

  1. Step 1: Understand what Box does

    A Box stores data on the heap, while the pointer itself lives on the stack.
  2. Step 2: Compare options with Box behavior

    Only To store data on the heap and keep a pointer on the stack correctly describes this behavior. Other options describe different concepts like cloning or raw pointers.
  3. Final Answer:

    To store data on the heap and keep a pointer on the stack -> Option C
  4. Quick Check:

    Box = heap data + stack pointer [OK]
Hint: Box always stores data on heap, pointer on stack [OK]
Common Mistakes:
  • Thinking Box allows multiple mutable references
  • Confusing Box with cloning or copying
  • Assuming Box replaces raw pointers
2. Which of the following is the correct syntax to create a Box containing the integer 10 in Rust?
easy
A. let b = Box::new(10);
B. let b = box 10;
C. let b = Box(10);
D. let b = new Box(10);

Solution

  1. Step 1: Recall Box creation syntax

    In Rust, Box::new(value) is the correct way to create a Box pointer.
  2. Step 2: Check each option

    let b = Box::new(10); uses Box::new(10), which is correct. let b = box 10; uses deprecated syntax. let b = Box(10); and let b = new Box(10); are invalid.
  3. Final Answer:

    let b = Box::new(10); -> Option A
  4. Quick Check:

    Box::new(value) creates a Box [OK]
Hint: Use Box::new(value) to create a Box pointer [OK]
Common Mistakes:
  • Using deprecated 'box' keyword
  • Trying to call Box like a function
  • Using 'new Box' which is invalid
3. What will be the output of this Rust code?
let b = Box::new(5);
println!("{}", *b + 3);
medium
A. Compilation error
B. 53
C. 5
D. 8

Solution

  1. Step 1: Understand dereferencing Box

    The *b dereferences the Box to get the value 5 stored on the heap.
  2. Step 2: Calculate the expression

    Adding 3 to 5 gives 8, so the printed output is 8.
  3. Final Answer:

    8 -> Option D
  4. Quick Check:

    *b + 3 = 8 [OK]
Hint: Dereference Box with * to access value [OK]
Common Mistakes:
  • Printing Box pointer without dereference
  • Concatenating instead of adding
  • Expecting compilation error due to Box
4. Identify the error in this Rust code using Box:
let b = Box::new(10);
let r1 = &b;
let r2 = &mut b;
println!("{} {}", r1, r2);
medium
A. Box::new requires a mutable variable
B. Cannot borrow `b` as mutable because it is already borrowed as immutable
C. Box cannot be printed with println!
D. No error, code runs fine

Solution

  1. Step 1: Analyze borrowing rules

    Rust does not allow mutable and immutable borrows at the same time. Here, r1 is an immutable borrow, and r2 is a mutable borrow of b.
  2. Step 2: Identify the error

    This causes a compile-time error: cannot borrow as mutable because already borrowed as immutable.
  3. Final Answer:

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

    Mutable + immutable borrow conflict = error [OK]
Hint: Cannot mix mutable and immutable borrows simultaneously [OK]
Common Mistakes:
  • Thinking Box cannot be printed
  • Assuming Box::new needs mut
  • Ignoring Rust's borrowing rules
5. Given this recursive enum using Box:
enum List {
    Cons(i32, Box<List>),
    Nil,
}

fn main() {
    let list = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
}
Why is Box necessary in this definition?
hard
A. To allocate recursive data on the heap and avoid infinite size
B. To allow multiple ownership of the list elements
C. To automatically clone the list elements
D. To make the enum implement Copy trait

Solution

  1. Step 1: Understand recursive enum size problem

    Recursive enums like List have infinite size if they directly contain themselves.
  2. Step 2: Role of Box in recursion

    Using Box stores the recursive part on the heap, giving a fixed pointer size on the stack, avoiding infinite size.
  3. Final Answer:

    To allocate recursive data on the heap and avoid infinite size -> Option A
  4. Quick Check:

    Box breaks infinite size recursion by heap allocation [OK]
Hint: Use Box to break infinite size in recursive types [OK]
Common Mistakes:
  • Thinking Box allows multiple ownership
  • Assuming Box clones data automatically
  • Believing Box makes enum Copy
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