Skip to content

Commit df81141

Browse files
author
AlexandruGG
committed
Address PR feedback: add tests
1 parent 7479a47 commit df81141

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

exercises/standard_library_types/box1.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,42 @@
1212
// Step 1: use a `Box` in the enum definition to make the code compile
1313
// Step 2: create both empty and non-empty cons lists of by replacing `unimplemented!()`
1414
//
15+
// Note: the tests should not be changed
16+
//
1517
// Execute `rustlings hint box1` for hints :)
1618

1719
// I AM NOT DONE
1820

1921
#[derive(PartialEq, Debug)]
20-
enum List {
22+
pub enum List {
2123
Cons(i32, List),
2224
Nil,
2325
}
2426

2527
fn main() {
26-
let empty_list = unimplemented!();
27-
println!("This is an empty cons list: {:?}", empty_list);
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::*;
2843

29-
let non_empty_list = unimplemented!();
30-
println!("This is a non-empty cons list: {:?}", non_empty_list);
44+
#[test]
45+
fn test_create_empty_list() {
46+
assert_eq!(List::Nil, create_empty_list())
47+
}
3148

32-
// Do not change these
33-
assert_eq!(List::Nil, empty_list);
34-
assert_ne!(empty_list, non_empty_list);
49+
#[test]
50+
fn test_create_non_empty_list() {
51+
assert_ne!(create_empty_list(), create_non_empty_list())
52+
}
3553
}

info.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,17 +617,17 @@ hint = """
617617
[[exercises]]
618618
name = "box1"
619619
path = "exercises/standard_library_types/box1.rs"
620-
mode = "compile"
620+
mode = "test"
621621
hint = """
622622
Step 1
623623
The compiler's message should help: since we cannot store the value of the actual type
624624
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:
625+
We should, therefore, place our `List` inside a `Box`. More details in the book here:
626626
https://doc.rust-lang.org/book/ch15-01-box.html#enabling-recursive-types-with-boxes
627627
628628
Step 2
629629
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".
630+
For a non-empty list keep in mind that we want to use our Cons "list builder".
631631
Although the current list is one of integers (i32), feel free to change the definition
632632
and try other types!
633633
"""

0 commit comments

Comments
 (0)