0
0
Rustprogramming~20 mins

RefCell overview in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RefCell 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 RefCell borrow example?
Consider this Rust code using RefCell. What will it print?
Rust
use std::cell::RefCell;

fn main() {
    let data = RefCell::new(5);
    {
        let borrow1 = data.borrow();
        println!("First borrow: {}", *borrow1);
    }
    {
        let mut borrow2 = data.borrow_mut();
        *borrow2 += 10;
        println!("Modified inside borrow_mut: {}", *borrow2);
    }
    println!("Final value: {}", data.borrow());
}
A
First borrow: 5
Modified inside borrow_mut: 15
Final value: 15
B
First borrow: 5
Modified inside borrow_mut: 10
Final value: 10
C
First borrow: 5
Modified inside borrow_mut: 15
Final value: 5
DCompilation error due to multiple borrows
Attempts:
2 left
💡 Hint
Remember that borrow() gives an immutable reference and borrow_mut() gives a mutable reference, but they cannot coexist.
🧠 Conceptual
intermediate
2:00remaining
What error occurs when borrowing RefCell incorrectly?
What error will this Rust code produce when run? use std::cell::RefCell; fn main() { let data = RefCell::new(10); let _borrow1 = data.borrow(); let _borrow2 = data.borrow_mut(); }
Acompile-time borrow checker error
Bpanic at runtime: already borrowed
Cno error, runs fine
Dsyntax error
Attempts:
2 left
💡 Hint
RefCell enforces borrowing rules at runtime, not compile time.
🔧 Debug
advanced
2:00remaining
Why does this RefCell code panic at runtime?
Identify the cause of the runtime panic in this code: use std::cell::RefCell; fn main() { let data = RefCell::new(vec![1, 2, 3]); let borrow1 = data.borrow(); data.borrow_mut().push(4); println!("{:?}", borrow1); }
ANo panic, code runs fine
BCannot push to vector inside RefCell
CImmutable borrow is dropped too early
DMutable borrow while immutable borrow is active causes panic
Attempts:
2 left
💡 Hint
Check the order and lifetime of borrows.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a RefCell with initial value 42?
Choose the correct Rust code to create a RefCell holding the integer 42.
Alet cell = RefCell::new(42);
Blet cell = RefCell(42);
Clet cell = RefCell::create(42);
Dlet cell = new RefCell(42);
Attempts:
2 left
💡 Hint
Use the standard constructor method for RefCell.
🚀 Application
expert
3:00remaining
How many items are in the vector after this RefCell code runs?
What is the length of the vector inside RefCell after this code executes? use std::cell::RefCell; fn main() { let data = RefCell::new(vec![1, 2]); { let mut borrow_mut = data.borrow_mut(); borrow_mut.push(3); } { let borrow = data.borrow(); println!("Vector contents: {:?}", *borrow); } { let mut borrow_mut = data.borrow_mut(); borrow_mut.push(4); } println!("Length: {}", data.borrow().len()); }
A2
B3
C4
DCompilation error
Attempts:
2 left
💡 Hint
Each mutable borrow adds one element to the vector.