0
0
Rustprogramming~10 mins

Why smart pointers are needed in Rust - Test Your Understanding

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

Complete the code to create a Box smart pointer.

Rust
let b = Box::new([1]);
Drag options to blanks, or click blank then click option'
Avec![1, 2, 3]
B&5
C5
DString::from("hello")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a reference instead of a value.
2fill in blank
medium

Complete the code to dereference a Box smart pointer.

Rust
let x = *[1];
Drag options to blanks, or click blank then click option'
Ab
BBox
C&b
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Dereferencing a value or a reference instead of the Box variable.
3fill in blank
hard

Fix the error in the code to use Rc smart pointer correctly.

Rust
use std::rc::Rc;
let a = Rc::new(5);
let b = Rc::clone(&[1]);
Drag options to blanks, or click blank then click option'
Ab
Ba
C&a
DRc
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable or a reference to the wrong type.
4fill in blank
hard

Fill both blanks to create a RefCell and borrow mutably.

Rust
use std::cell::RefCell;
let data = RefCell::new([1]);
let mut val = data.[2]();
Drag options to blanks, or click blank then click option'
A5
Bborrow_mut
Cborrow
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using borrow() instead of borrow_mut() for mutable access.
5fill in blank
hard

Fill all three blanks to create a smart pointer, borrow immutably, and print the value.

Rust
use std::rc::Rc;
let value = Rc::new([1]);
let borrowed = value.[2]();
println!("Value: {}", [3]);
Drag options to blanks, or click blank then click option'
A10
Bclone
Cborrowed
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong value or forgetting to clone Rc.