0
0
Rustprogramming~10 mins

RefCell overview in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new RefCell containing the value 5.

Rust
let value = std::cell::[1]::new(5);
Drag options to blanks, or click blank then click option'
ARefCell
BCell
CMutex
DRc
Attempts:
3 left
💡 Hint
Common Mistakes
Using Cell instead of RefCell, which has different borrowing rules.
Using Rc which is for reference counting, not interior mutability.
2fill in blank
medium

Complete the code to borrow the value immutably from the RefCell.

Rust
let value = std::cell::RefCell::new(10);
let borrowed = value.[1]();
Drag options to blanks, or click blank then click option'
Aborrow_mut
Bborrow
Cget
Dclone
Attempts:
3 left
💡 Hint
Common Mistakes
Using borrow_mut() when only an immutable borrow is needed.
Trying to use get(), which is not a method of RefCell.
3fill in blank
hard

Fix the error in the code to mutably borrow the value inside the RefCell.

Rust
let cell = std::cell::RefCell::new(20);
let mut_ref = cell.[1]();
Drag options to blanks, or click blank then click option'
Aborrow_mut
Bborrow
Cget_mut
Dclone
Attempts:
3 left
💡 Hint
Common Mistakes
Using borrow() instead of borrow_mut() for mutable access.
Trying to use get_mut(), which is not available on RefCell.
4fill in blank
hard

Fill both blanks to create a RefCell, mutably borrow its value, and change it.

Rust
let cell = std::cell::[1]::new(30);
let mut val = cell.[2]();
*val = 40;
Drag options to blanks, or click blank then click option'
ARefCell
Bborrow
Cborrow_mut
DCell
Attempts:
3 left
💡 Hint
Common Mistakes
Using Cell instead of RefCell, which has different borrowing rules.
Using borrow() instead of borrow_mut() when trying to mutate.
5fill in blank
hard

Fill all three blanks to create a RefCell, borrow it immutably, and print the value.

Rust
let cell = std::cell::[1]::new(50);
let val = cell.[2]();
println!("Value is: {}", *[3]);
Drag options to blanks, or click blank then click option'
ACell
Bborrow
Cval
DRefCell
Attempts:
3 left
💡 Hint
Common Mistakes
Using Cell instead of RefCell.
Trying to print the RefCell itself instead of the borrowed value.