Skip to content

Commit 7479a47

Browse files
author
AlexandruGG
committed
feat: Add box1.rs exercise
1 parent 06ef4cc commit 7479a47

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

exercises/standard_library_types/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
For the Box exercise check out the chapter [Using Box to Point to Data on the Heap](https://doc.rust-lang.org/book/ch15-01-box.html).
2+
13
For the Arc exercise check out the chapter [Shared-State Concurrency](https://doc.rust-lang.org/book/ch16-03-shared-state.html) of the Rust Book.
24

35
For the Iterator exercise check out the chapters [Iterator](https://doc.rust-lang.org/book/ch13-02-iterators.html) of the Rust Book and the [Iterator documentation](https://doc.rust-lang.org/stable/std/iter/).
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// box1.rs
2+
//
3+
// At compile time, Rust needs to know how much space a type takes up. This becomes problematic
4+
// for recursive types, where a value can have as part of itself another value of the same type.
5+
// To get around the issue, we can use a `Box` - a smart pointer used to store data on the heap,
6+
// which also allows us to wrap a recursive type.
7+
//
8+
// The recursive type we're implementing in this exercise is the `cons list` - a data structure
9+
// frequently found in functional programming languages. Each item in a cons list contains two
10+
// elements: the value of the current item and the next item. The last item is a value called `Nil`.
11+
//
12+
// Step 1: use a `Box` in the enum definition to make the code compile
13+
// Step 2: create both empty and non-empty cons lists of by replacing `unimplemented!()`
14+
//
15+
// Execute `rustlings hint box1` for hints :)
16+
17+
// I AM NOT DONE
18+
19+
#[derive(PartialEq, Debug)]
20+
enum List {
21+
Cons(i32, List),
22+
Nil,
23+
}
24+
25+
fn main() {
26+
let empty_list = unimplemented!();
27+
println!("This is an empty cons list: {:?}", empty_list);
28+
29+
let non_empty_list = unimplemented!();
30+
println!("This is a non-empty cons list: {:?}", non_empty_list);
31+
32+
// Do not change these
33+
assert_eq!(List::Nil, empty_list);
34+
assert_ne!(empty_list, non_empty_list);
35+
}

info.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,24 @@ hint = """
614614

615615
# STANDARD LIBRARY TYPES
616616

617+
[[exercises]]
618+
name = "box1"
619+
path = "exercises/standard_library_types/box1.rs"
620+
mode = "compile"
621+
hint = """
622+
Step 1
623+
The compiler's message should help: since we cannot store the value of the actual type
624+
when working with recursive types, we need to store a reference (pointer) to its value.
625+
We should therefore place our `List` inside a `Box`. More details in the book here:
626+
https://doc.rust-lang.org/book/ch15-01-box.html#enabling-recursive-types-with-boxes
627+
628+
Step 2
629+
Creating an empty list should be fairly straightforward (hint: peek at the assertions).
630+
For a non-empty list keep in mind that wee want to use our Cons "list builder".
631+
Although the current list is one of integers (i32), feel free to change the definition
632+
and try other types!
633+
"""
634+
617635
[[exercises]]
618636
name = "arc1"
619637
path = "exercises/standard_library_types/arc1.rs"

0 commit comments

Comments
 (0)