0
0
Rustprogramming~10 mins

Box pointer 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 Box pointer holding the value 5.

Rust
let b = [1]::new(5);
Drag options to blanks, or click blank then click option'
AArc
BRc
CBox
DVec
Attempts:
3 left
💡 Hint
Common Mistakes
Using Vec instead of Box, which creates a vector, not a pointer.
Using Rc or Arc which are reference counted pointers, not simple boxes.
2fill in blank
medium

Complete the code to dereference the Box pointer and print its value.

Rust
let b = Box::new(10);
println!("{}", *[1]);
Drag options to blanks, or click blank then click option'
ABox
Bb
C&b
Db.as_ref()
Attempts:
3 left
💡 Hint
Common Mistakes
Using &b which is a reference to the Box, not the value.
Using b.as_ref() which returns a reference, not the value itself.
3fill in blank
hard

Fix the error in the code by completing the blank to create a Box pointer to a string.

Rust
let s = [1]::new(String::from("hello"));
Drag options to blanks, or click blank then click option'
AArc
BVec
CRc
DBox
Attempts:
3 left
💡 Hint
Common Mistakes
Using Vec which is a growable array, not a pointer.
Using Rc or Arc which are reference counted pointers.
4fill in blank
hard

Fill both blanks to create a Box pointer to an integer and then dereference it to get the value.

Rust
let b = [1]::new(42);
let val = *[2];
Drag options to blanks, or click blank then click option'
ABox
Bb
CRc
DArc
Attempts:
3 left
💡 Hint
Common Mistakes
Using Rc or Arc instead of Box for simple ownership.
Dereferencing the type name instead of the variable.
5fill in blank
hard

Fill all three blanks to create a Box pointer to a vector, push a value, and print the length.

Rust
let mut v = [1]::new(vec![1, 2, 3]);
v.[2](4);
println!("Length: {}", v.[3]());
Drag options to blanks, or click blank then click option'
ABox
Bpush
Clen
DVec
Attempts:
3 left
💡 Hint
Common Mistakes
Using Vec::new instead of Box::new to create the pointer.
Calling push or len on the Box pointer without dereferencing (but in Rust, Box auto-derefs).