mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-23 22:22:22 -05:00
box1 solution
This commit is contained in:
parent
f53d458920
commit
61c7eaed62
3 changed files with 66 additions and 27 deletions
|
@ -4,45 +4,43 @@
|
|||
// `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
|
||||
// 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
|
||||
// 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`.
|
||||
//
|
||||
// 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)]
|
||||
enum List {
|
||||
Cons(i32, 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()
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: Create an empty cons list.
|
||||
fn create_empty_list() -> List {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// TODO: Create a non-empty cons list.
|
||||
fn create_non_empty_list() -> List {
|
||||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_create_empty_list() {
|
||||
assert_eq!(List::Nil, create_empty_list());
|
||||
assert_eq!(create_empty_list(), List::Nil);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -969,21 +969,16 @@ a different method that could make your code more compact than using `fold`."""
|
|||
name = "box1"
|
||||
dir = "19_smart_pointers"
|
||||
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
|
||||
(pointer) to its value.
|
||||
|
||||
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
|
||||
We should, therefore, place our `List` inside a `Box`. More details in The Book:
|
||||
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
|
||||
assertions).
|
||||
|
||||
For a non-empty list keep in mind that we want to use our `Cons` "list builder".
|
||||
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
|
||||
definition and try other types!"""
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue