0
0
Rustprogramming~10 mins

Ownership with smart pointers 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 Box smart pointer holding the value 5.

Rust
let b = Box::[1](5);
Drag options to blanks, or click blank then click option'
Anew
Bcreate
Cmake
Dbuild
Attempts:
3 left
💡 Hint
Common Mistakes
Using functions like create or make which do not exist for Box.
Trying to call Box as a function directly.
2fill in blank
medium

Complete the code to clone a Rc smart pointer.

Rust
let rc2 = Rc::[1](&rc1);
Drag options to blanks, or click blank then click option'
Acopy
Bclone
Cduplicate
Dreplicate
Attempts:
3 left
💡 Hint
Common Mistakes
Using copy which is not implemented for Rc.
Trying to use duplicate or replicate which do not exist.
3fill in blank
hard

Fix the error in the code by completing the blank to get a mutable reference from a RefCell.

Rust
let mut_ref = refcell.[1]().unwrap();
Drag options to blanks, or click blank then click option'
Aborrow_mut
Bborrow
Cget_mut
Dtake_mut
Attempts:
3 left
💡 Hint
Common Mistakes
Using borrow which only gives an immutable reference.
Trying to use get_mut which requires exclusive ownership.
4fill in blank
hard

Fill both blanks to create a HashMap from strings to their lengths, only including words longer than 3 characters.

Rust
let lengths = words.iter().filter(|&word| word.len() [1] 3).map(|word| (word.to_string(), word.[2]())).collect::<HashMap<_, _>>();
Drag options to blanks, or click blank then click option'
A>
Blen
C<
Dis_empty
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the filter condition.
Using is_empty which returns a boolean, not length.
5fill in blank
hard

Fill both blanks to create a HashMap from uppercase keys to values, including only entries with positive values.

Rust
let result = data.iter().map(|(k, v)| (k.[1](), *v)).filter(|(_, v)| *v [2] 0).collect::<HashMap<_, _>>();
Drag options to blanks, or click blank then click option'
Ato_uppercase
B>
C==
Dto_lowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_lowercase instead of uppercase.
Filtering values equal to zero instead of greater than zero.