Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Box smart pointer.
Rust
let b = Box::new([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a reference instead of a value.
✗ Incorrect
Box::new takes ownership of a value, here the integer 5.
2fill in blank
mediumComplete the code to dereference a Box smart pointer.
Rust
let x = *[1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Dereferencing a value or a reference instead of the Box variable.
✗ Incorrect
Dereferencing a Box requires the variable holding the Box, here 'b'.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable or a reference to the wrong type.
✗ Incorrect
Rc::clone takes a reference to the Rc smart pointer variable, here &a.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using borrow() instead of borrow_mut() for mutable access.
✗ Incorrect
RefCell::new wraps a value, here 5, and borrow_mut() allows mutable access.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong value or forgetting to clone Rc.
✗ Incorrect
Rc::new wraps 5, clone() creates a new pointer, and borrowed is printed.