Skip to content

Commit 5f08069

Browse files
author
fmoko
authored
Merge pull request #409 from AlexandruGG/feature/box-exercise
2 parents 918f310 + 7e79c51 commit 5f08069

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ target/
55
*.pdb
66
exercises/clippy/Cargo.toml
77
exercises/clippy/Cargo.lock
8+
.idea

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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
// Note: the tests should not be changed
16+
//
17+
// Execute `rustlings hint box1` for hints :)
18+
19+
// I AM NOT DONE
20+
21+
#[derive(PartialEq, Debug)]
22+
pub enum List {
23+
Cons(i32, List),
24+
Nil,
25+
}
26+
27+
fn main() {
28+
println!("This is an empty cons list: {:?}", create_empty_list());
29+
println!("This is a non-empty cons list: {:?}", create_non_empty_list());
30+
}
31+
32+
pub fn create_empty_list() -> List {
33+
unimplemented!()
34+
}
35+
36+
pub fn create_non_empty_list() -> List {
37+
unimplemented!()
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::*;
43+
44+
#[test]
45+
fn test_create_empty_list() {
46+
assert_eq!(List::Nil, create_empty_list())
47+
}
48+
49+
#[test]
50+
fn test_create_non_empty_list() {
51+
assert_ne!(create_empty_list(), create_non_empty_list())
52+
}
53+
}

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 = "test"
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 we 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)