box1 solution

This commit is contained in:
mo8it 2024-06-28 21:24:35 +02:00
parent f53d458920
commit 61c7eaed62
3 changed files with 66 additions and 27 deletions

View file

@ -4,45 +4,43 @@
// `Box` - a smart pointer used to store data on the heap, which also allows us // `Box` - a smart pointer used to store data on the heap, which also allows us
// to wrap a recursive type. // to wrap a recursive type.
// //
// The recursive type we're implementing in this exercise is the `cons list` - a // The recursive type we're implementing in this exercise is the "cons list", a
// data structure frequently found in functional programming languages. Each // data structure frequently found in functional programming languages. Each
// item in a cons list contains two elements: the value of the current item and // item in a cons list contains two elements: The value of the current item and
// the next item. The last item is a value called `Nil`. // the next item. The last item is a value called `Nil`.
//
// Step 1: use a `Box` in the enum definition to make the code compile
// Step 2: create both empty and non-empty cons lists by replacing `todo!()`
//
// Note: the tests should not be changed
// TODO: Use a `Box` in the enum definition to make the code compile.
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
enum List { enum List {
Cons(i32, List), Cons(i32, List),
Nil, Nil,
} }
fn main() { // TODO: Create an empty cons list.
println!("This is an empty cons list: {:?}", create_empty_list());
println!(
"This is a non-empty cons list: {:?}",
create_non_empty_list()
);
}
fn create_empty_list() -> List { fn create_empty_list() -> List {
todo!() todo!()
} }
// TODO: Create a non-empty cons list.
fn create_non_empty_list() -> List { fn create_non_empty_list() -> List {
todo!() todo!()
} }
fn main() {
println!("This is an empty cons list: {:?}", create_empty_list());
println!(
"This is a non-empty cons list: {:?}",
create_non_empty_list(),
);
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_create_empty_list() { fn test_create_empty_list() {
assert_eq!(List::Nil, create_empty_list()); assert_eq!(create_empty_list(), List::Nil);
} }
#[test] #[test]

View file

@ -969,21 +969,16 @@ a different method that could make your code more compact than using `fold`."""
name = "box1" name = "box1"
dir = "19_smart_pointers" dir = "19_smart_pointers"
hint = """ hint = """
Step 1: The compiler's message should help: Since we cannot store the value of the
The compiler's message should help: since we cannot store the value of the
actual type when working with recursive types, we need to store a reference actual type when working with recursive types, we need to store a reference
(pointer) to its value. (pointer) to its value.
We should, therefore, place our `List` inside a `Box`. More details in the book We should, therefore, place our `List` inside a `Box`. More details in The Book:
here: https://doc.rust-lang.org/book/ch15-01-box.html#enabling-recursive-types-with-boxes https://doc.rust-lang.org/book/ch15-01-box.html#enabling-recursive-types-with-boxes
Step 2: Creating an empty list should be fairly straightforward (Hint: Read the tests).
Creating an empty list should be fairly straightforward (hint: peek at the For a non-empty list, keep in mind that we want to use our `Cons` list builder.
assertions).
For a non-empty list keep in mind that we want to use our `Cons` "list builder".
Although the current list is one of integers (`i32`), feel free to change the Although the current list is one of integers (`i32`), feel free to change the
definition and try other types!""" definition and try other types!"""

View file

@ -1 +1,47 @@
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 // At compile time, Rust needs to know how much space a type takes up. This
// becomes problematic for recursive types, where a value can have as part of
// itself another value of the same type. To get around the issue, we can use a
// `Box` - a smart pointer used to store data on the heap, which also allows us
// to wrap a recursive type.
//
// The recursive type we're implementing in this exercise is the "cons list", a
// data structure frequently found in functional programming languages. Each
// item in a cons list contains two elements: The value of the current item and
// the next item. The last item is a value called `Nil`.
#[derive(PartialEq, Debug)]
enum List {
Cons(i32, Box<List>),
Nil,
}
fn create_empty_list() -> List {
List::Nil
}
fn create_non_empty_list() -> List {
List::Cons(42, Box::new(List::Nil))
}
fn main() {
println!("This is an empty cons list: {:?}", create_empty_list());
println!(
"This is a non-empty cons list: {:?}",
create_non_empty_list(),
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_empty_list() {
assert_eq!(create_empty_list(), List::Nil);
}
#[test]
fn test_create_non_empty_list() {
assert_ne!(create_empty_list(), create_non_empty_list());
}
}