mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-09 20:03:24 +03:00
Compare commits
7 commits
746cf6863d
...
61c7eaed62
Author | SHA1 | Date | |
---|---|---|---|
61c7eaed62 | |||
f53d458920 | |||
2af437fd90 | |||
56a9197f55 | |||
eddbb97934 | |||
4f71f74b44 | |||
cf9041c0e4 |
|
@ -29,8 +29,8 @@ mod tests {
|
||||||
// TODO: This test should check if the rectangle has the size that we
|
// TODO: This test should check if the rectangle has the size that we
|
||||||
// pass to its constructor.
|
// pass to its constructor.
|
||||||
let rect = Rectangle::new(10, 20);
|
let rect = Rectangle::new(10, 20);
|
||||||
assert_eq!(???, 10); // Check width
|
assert_eq!(todo!(), 10); // Check width
|
||||||
assert_eq!(???, 20); // Check height
|
assert_eq!(todo!(), 20); // Check height
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This test should check if the program panics when we try to create
|
// TODO: This test should check if the program panics when we try to create
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
// When performing operations on elements within a collection, iterators are
|
// When performing operations on elements within a collection, iterators are
|
||||||
// essential. This module helps you get familiar with the structure of using an
|
// essential. This module helps you get familiar with the structure of using an
|
||||||
// iterator and how to go through elements within an iterable collection.
|
// iterator and how to go through elements within an iterable collection.
|
||||||
//
|
|
||||||
// Make me compile by filling in the `???`s
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
|
@ -10,19 +8,18 @@ fn main() {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn iterators() {
|
fn iterators() {
|
||||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||||
|
|
||||||
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
// TODO: Create an iterator over the array.
|
||||||
|
let mut fav_fruits_iterator = todo!();
|
||||||
|
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
|
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,28 @@
|
||||||
// In this exercise, you'll learn some of the unique advantages that iterators
|
// In this exercise, you'll learn some of the unique advantages that iterators
|
||||||
// can offer. Follow the steps to complete the exercise.
|
// can offer.
|
||||||
|
|
||||||
// Step 1.
|
// TODO: Complete the `capitalize_first` function.
|
||||||
// Complete the `capitalize_first` function.
|
|
||||||
// "hello" -> "Hello"
|
// "hello" -> "Hello"
|
||||||
fn capitalize_first(input: &str) -> String {
|
fn capitalize_first(input: &str) -> String {
|
||||||
let mut c = input.chars();
|
let mut chars = input.chars();
|
||||||
match c.next() {
|
match chars.next() {
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
Some(first) => ???,
|
Some(first) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2.
|
// TODO: Apply the `capitalize_first` function to a slice of string slices.
|
||||||
// Apply the `capitalize_first` function to a slice of string slices.
|
|
||||||
// Return a vector of strings.
|
// Return a vector of strings.
|
||||||
// ["hello", "world"] -> ["Hello", "World"]
|
// ["hello", "world"] -> ["Hello", "World"]
|
||||||
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
vec![]
|
// ???
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3.
|
// TODO: Apply the `capitalize_first` function again to a slice of string
|
||||||
// Apply the `capitalize_first` function again to a slice of string slices.
|
// slices. Return a single string.
|
||||||
// Return a single string.
|
|
||||||
// ["hello", " ", "world"] -> "Hello World"
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
fn capitalize_words_string(words: &[&str]) -> String {
|
fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
String::new()
|
// ???
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -1,40 +1,26 @@
|
||||||
// This is a bigger exercise than most of the others! You can do it! Here is
|
|
||||||
// your mission, should you choose to accept it:
|
|
||||||
// 1. Complete the divide function to get the first four tests to pass.
|
|
||||||
// 2. Get the remaining tests to pass by completing the result_with_list and
|
|
||||||
// list_of_results functions.
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
enum DivisionError {
|
enum DivisionError {
|
||||||
NotDivisible(NotDivisibleError),
|
|
||||||
DivideByZero,
|
DivideByZero,
|
||||||
|
NotDivisible,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||||
struct NotDivisibleError {
|
|
||||||
dividend: i32,
|
|
||||||
divisor: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
|
||||||
// Otherwise, return a suitable error.
|
// Otherwise, return a suitable error.
|
||||||
fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test
|
// TODO: Add the correct return type and complete the function body.
|
||||||
// passes.
|
// Desired output: `Ok([1, 11, 1426, 3])`
|
||||||
// Desired output: Ok([1, 11, 1426, 3])
|
fn result_with_list() {
|
||||||
fn result_with_list() -> () {
|
let numbers = [27, 297, 38502, 81];
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test
|
// TODO: Add the correct return type and complete the function body.
|
||||||
// passes.
|
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
|
||||||
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
fn list_of_results() {
|
||||||
fn list_of_results() -> () {
|
let numbers = [27, 297, 38502, 81];
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,19 +38,13 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_not_divisible() {
|
fn test_divide_by_0() {
|
||||||
assert_eq!(
|
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
|
||||||
divide(81, 6),
|
|
||||||
Err(DivisionError::NotDivisible(NotDivisibleError {
|
|
||||||
dividend: 81,
|
|
||||||
divisor: 6
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_divide_by_0() {
|
fn test_not_divisible() {
|
||||||
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
|
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -74,14 +54,11 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_result_with_list() {
|
fn test_result_with_list() {
|
||||||
assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
|
assert_eq!(result_with_list().unwarp(), [1, 11, 1426, 3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_list_of_results() {
|
fn test_list_of_results() {
|
||||||
assert_eq!(
|
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
|
||||||
format!("{:?}", list_of_results()),
|
|
||||||
"[Ok(1), Ok(11), Ok(1426), Ok(3)]"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
fn factorial(num: u64) -> u64 {
|
fn factorial(num: u8) -> u64 {
|
||||||
// Complete this function to return the factorial of num
|
// TODO: Complete this function to return the factorial of `num`.
|
||||||
// Do not use:
|
// Do not use:
|
||||||
// - early returns (using the `return` keyword explicitly)
|
// - early returns (using the `return` keyword explicitly)
|
||||||
// Try not to use:
|
// Try not to use:
|
||||||
// - imperative style loops (for, while)
|
// - imperative style loops (for/while)
|
||||||
// - additional variables
|
// - additional variables
|
||||||
// For an extra challenge, don't use:
|
// For an extra challenge, don't use:
|
||||||
// - recursion
|
// - recursion
|
||||||
|
@ -19,20 +19,20 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn factorial_of_0() {
|
fn factorial_of_0() {
|
||||||
assert_eq!(1, factorial(0));
|
assert_eq!(factorial(0), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn factorial_of_1() {
|
fn factorial_of_1() {
|
||||||
assert_eq!(1, factorial(1));
|
assert_eq!(factorial(1), 1);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn factorial_of_2() {
|
fn factorial_of_2() {
|
||||||
assert_eq!(2, factorial(2));
|
assert_eq!(factorial(2), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn factorial_of_4() {
|
fn factorial_of_4() {
|
||||||
assert_eq!(24, factorial(4));
|
assert_eq!(factorial(4), 24);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
// Let's define a simple model to track Rustlings exercise progress. Progress
|
// Let's define a simple model to track Rustlings' exercise progress. Progress
|
||||||
// will be modelled using a hash map. The name of the exercise is the key and
|
// will be modelled using a hash map. The name of the exercise is the key and
|
||||||
// the progress is the value. Two counting functions were created to count the
|
// the progress is the value. Two counting functions were created to count the
|
||||||
// number of exercises with a given progress. Recreate this counting
|
// number of exercises with a given progress. Recreate this counting
|
||||||
// functionality using iterators. Try not to use imperative loops (for, while).
|
// functionality using iterators. Try to not use imperative loops (for/while).
|
||||||
// Only the two iterator methods (count_iterator and count_collection_iterator)
|
|
||||||
// need to be modified.
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
@ -18,24 +16,25 @@ enum Progress {
|
||||||
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for val in map.values() {
|
for val in map.values() {
|
||||||
if val == &value {
|
if *val == value {
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Implement the functionality of `count_for` but with an iterator instead
|
||||||
|
// of a `for` loop.
|
||||||
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
// map is a hashmap with String keys and Progress values.
|
// `map` is a hash map with `String` keys and `Progress` values.
|
||||||
// map = { "variables1": Complete, "from_str": None, ... }
|
// map = { "variables1": Complete, "from_str": None, … }
|
||||||
todo!();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for map in collection {
|
for map in collection {
|
||||||
for val in map.values() {
|
for val in map.values() {
|
||||||
if val == &value {
|
if *val == value {
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,11 +42,12 @@ fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progres
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Implement the functionality of `count_collection_for` but with an
|
||||||
|
// iterator instead of a `for` loop.
|
||||||
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||||
// collection is a slice of hashmaps.
|
// `collection` is a slice of hash maps.
|
||||||
// collection = [{ "variables1": Complete, "from_str": None, ... },
|
// collection = [{ "variables1": Complete, "from_str": None, … },
|
||||||
// { "variables2": Complete, ... }, ... ]
|
// { "variables2": Complete, … }, … ]
|
||||||
todo!();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -58,70 +58,6 @@ fn main() {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_complete() {
|
|
||||||
let map = get_map();
|
|
||||||
assert_eq!(3, count_iterator(&map, Progress::Complete));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_some() {
|
|
||||||
let map = get_map();
|
|
||||||
assert_eq!(1, count_iterator(&map, Progress::Some));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_none() {
|
|
||||||
let map = get_map();
|
|
||||||
assert_eq!(2, count_iterator(&map, Progress::None));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_complete_equals_for() {
|
|
||||||
let map = get_map();
|
|
||||||
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
|
|
||||||
for progress_state in progress_states {
|
|
||||||
assert_eq!(
|
|
||||||
count_for(&map, progress_state),
|
|
||||||
count_iterator(&map, progress_state)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_collection_complete() {
|
|
||||||
let collection = get_vec_map();
|
|
||||||
assert_eq!(
|
|
||||||
6,
|
|
||||||
count_collection_iterator(&collection, Progress::Complete)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_collection_some() {
|
|
||||||
let collection = get_vec_map();
|
|
||||||
assert_eq!(1, count_collection_iterator(&collection, Progress::Some));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_collection_none() {
|
|
||||||
let collection = get_vec_map();
|
|
||||||
assert_eq!(4, count_collection_iterator(&collection, Progress::None));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn count_collection_equals_for() {
|
|
||||||
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
|
|
||||||
let collection = get_vec_map();
|
|
||||||
|
|
||||||
for progress_state in progress_states {
|
|
||||||
assert_eq!(
|
|
||||||
count_collection_for(&collection, progress_state),
|
|
||||||
count_collection_iterator(&collection, progress_state)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_map() -> HashMap<String, Progress> {
|
fn get_map() -> HashMap<String, Progress> {
|
||||||
use Progress::*;
|
use Progress::*;
|
||||||
|
|
||||||
|
@ -150,4 +86,68 @@ mod tests {
|
||||||
|
|
||||||
vec![map, other]
|
vec![map, other]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_complete() {
|
||||||
|
let map = get_map();
|
||||||
|
assert_eq!(count_iterator(&map, Progress::Complete), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_some() {
|
||||||
|
let map = get_map();
|
||||||
|
assert_eq!(count_iterator(&map, Progress::Some), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_none() {
|
||||||
|
let map = get_map();
|
||||||
|
assert_eq!(count_iterator(&map, Progress::None), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_complete_equals_for() {
|
||||||
|
let map = get_map();
|
||||||
|
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
|
||||||
|
for progress_state in progress_states {
|
||||||
|
assert_eq!(
|
||||||
|
count_for(&map, progress_state),
|
||||||
|
count_iterator(&map, progress_state),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_complete() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
assert_eq!(
|
||||||
|
count_collection_iterator(&collection, Progress::Complete),
|
||||||
|
6,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_some() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
assert_eq!(count_collection_iterator(&collection, Progress::Some), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_none() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
assert_eq!(count_collection_iterator(&collection, Progress::None), 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_equals_for() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
|
||||||
|
|
||||||
|
for progress_state in progress_states {
|
||||||
|
assert_eq!(
|
||||||
|
count_collection_for(&collection, progress_state),
|
||||||
|
count_collection_iterator(&collection, progress_state),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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]
|
||||||
|
|
|
@ -886,28 +886,15 @@ https://doc.rust-lang.org/stable/book/ch11-01-writing-tests.html#checking-for-pa
|
||||||
name = "iterators1"
|
name = "iterators1"
|
||||||
dir = "18_iterators"
|
dir = "18_iterators"
|
||||||
hint = """
|
hint = """
|
||||||
Step 1:
|
|
||||||
|
|
||||||
We need to apply something to the collection `my_fav_fruits` before we start to
|
|
||||||
go through it. What could that be? Take a look at the struct definition for a
|
|
||||||
vector for inspiration:
|
|
||||||
https://doc.rust-lang.org/std/vec/struct.Vec.html
|
|
||||||
|
|
||||||
Step 2 & step 3:
|
|
||||||
|
|
||||||
Very similar to the lines above and below. You've got this!
|
|
||||||
|
|
||||||
Step 4:
|
|
||||||
|
|
||||||
An iterator goes through all elements in a collection, but what if we've run
|
An iterator goes through all elements in a collection, but what if we've run
|
||||||
out of elements? What should we expect here? If you're stuck, take a look at
|
out of elements? What should we expect here? If you're stuck, take a look at
|
||||||
https://doc.rust-lang.org/std/iter/trait.Iterator.html for some ideas."""
|
https://doc.rust-lang.org/std/iter/trait.Iterator.html"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "iterators2"
|
name = "iterators2"
|
||||||
dir = "18_iterators"
|
dir = "18_iterators"
|
||||||
hint = """
|
hint = """
|
||||||
Step 1:
|
`capitalize_first`:
|
||||||
|
|
||||||
The variable `first` is a `char`. It needs to be capitalized and added to the
|
The variable `first` is a `char`. It needs to be capitalized and added to the
|
||||||
remaining characters in `c` in order to return the correct `String`.
|
remaining characters in `c` in order to return the correct `String`.
|
||||||
|
@ -918,12 +905,15 @@ The remaining characters in `c` can be viewed as a string slice using the
|
||||||
The documentation for `char` contains many useful methods.
|
The documentation for `char` contains many useful methods.
|
||||||
https://doc.rust-lang.org/std/primitive.char.html
|
https://doc.rust-lang.org/std/primitive.char.html
|
||||||
|
|
||||||
Step 2:
|
Use `char::to_uppercase`. It returns an iterator that can be converted to a
|
||||||
|
`String`.
|
||||||
|
|
||||||
|
`capitalize_words_vector`:
|
||||||
|
|
||||||
Create an iterator from the slice. Transform the iterated values by applying
|
Create an iterator from the slice. Transform the iterated values by applying
|
||||||
the `capitalize_first` function. Remember to `collect` the iterator.
|
the `capitalize_first` function. Remember to `collect` the iterator.
|
||||||
|
|
||||||
Step 3:
|
`capitalize_words_string`:
|
||||||
|
|
||||||
This is surprisingly similar to the previous solution. `collect` is very
|
This is surprisingly similar to the previous solution. `collect` is very
|
||||||
powerful and very general. Rust just needs to know the desired type."""
|
powerful and very general. Rust just needs to know the desired type."""
|
||||||
|
@ -932,8 +922,8 @@ powerful and very general. Rust just needs to know the desired type."""
|
||||||
name = "iterators3"
|
name = "iterators3"
|
||||||
dir = "18_iterators"
|
dir = "18_iterators"
|
||||||
hint = """
|
hint = """
|
||||||
The `divide` function needs to return the correct error when even division is
|
The `divide` function needs to return the correct error when the divisor is 0 or
|
||||||
not possible.
|
when even division is not possible.
|
||||||
|
|
||||||
The `division_results` variable needs to be collected into a collection type.
|
The `division_results` variable needs to be collected into a collection type.
|
||||||
|
|
||||||
|
@ -944,7 +934,7 @@ The `list_of_results` function needs to return a vector of results.
|
||||||
|
|
||||||
See https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect for
|
See https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect for
|
||||||
how the `FromIterator` trait is used in `collect()`. This trait is REALLY
|
how the `FromIterator` trait is used in `collect()`. This trait is REALLY
|
||||||
powerful! It can make the solution to this exercise infinitely easier."""
|
powerful! It can make the solution to this exercise much easier."""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "iterators4"
|
name = "iterators4"
|
||||||
|
@ -952,10 +942,10 @@ dir = "18_iterators"
|
||||||
hint = """
|
hint = """
|
||||||
In an imperative language, you might write a `for` loop that updates a mutable
|
In an imperative language, you might write a `for` loop that updates a mutable
|
||||||
variable. Or, you might write code utilizing recursion and a match clause. In
|
variable. Or, you might write code utilizing recursion and a match clause. In
|
||||||
Rust you can take another functional approach, computing the factorial
|
Rust, you can take another functional approach, computing the factorial
|
||||||
elegantly with ranges and iterators.
|
elegantly with ranges and iterators.
|
||||||
|
|
||||||
Hint 2: Check out the `fold` and `rfold` methods!"""
|
Check out the `fold` and `rfold` methods!"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "iterators5"
|
name = "iterators5"
|
||||||
|
@ -979,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!"""
|
||||||
|
|
||||||
|
|
|
@ -1 +1,26 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
// When performing operations on elements within a collection, iterators are
|
||||||
|
// essential. This module helps you get familiar with the structure of using an
|
||||||
|
// iterator and how to go through elements within an iterable collection.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn iterators() {
|
||||||
|
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||||
|
|
||||||
|
// Create an iterator over the array.
|
||||||
|
let mut fav_fruits_iterator = my_fav_fruits.iter();
|
||||||
|
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"peach"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
|
||||||
|
assert_eq!(fav_fruits_iterator.next(), None);
|
||||||
|
// ^^^^ reached the end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,56 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
// In this exercise, you'll learn some of the unique advantages that iterators
|
||||||
|
// can offer.
|
||||||
|
|
||||||
|
// "hello" -> "Hello"
|
||||||
|
fn capitalize_first(input: &str) -> String {
|
||||||
|
let mut chars = input.chars();
|
||||||
|
match chars.next() {
|
||||||
|
None => String::new(),
|
||||||
|
Some(first) => first.to_uppercase().to_string() + chars.as_str(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the `capitalize_first` function to a slice of string slices.
|
||||||
|
// Return a vector of strings.
|
||||||
|
// ["hello", "world"] -> ["Hello", "World"]
|
||||||
|
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
|
words.iter().map(|word| capitalize_first(word)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the `capitalize_first` function again to a slice of string
|
||||||
|
// slices. Return a single string.
|
||||||
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
|
fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
|
words.iter().map(|word| capitalize_first(word)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_success() {
|
||||||
|
assert_eq!(capitalize_first("hello"), "Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_empty() {
|
||||||
|
assert_eq!(capitalize_first(""), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_iterate_string_vec() {
|
||||||
|
let words = vec!["hello", "world"];
|
||||||
|
assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_iterate_into_string() {
|
||||||
|
let words = vec!["hello", " ", "world"];
|
||||||
|
assert_eq!(capitalize_words_string(&words), "Hello World");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,73 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
enum DivisionError {
|
||||||
|
DivideByZero,
|
||||||
|
NotDivisible,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
|
||||||
|
if b == 0 {
|
||||||
|
return Err(DivisionError::DivideByZero);
|
||||||
|
}
|
||||||
|
|
||||||
|
if a % b != 0 {
|
||||||
|
return Err(DivisionError::NotDivisible);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(a / b)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn result_with_list() -> Result<Vec<i64>, DivisionError> {
|
||||||
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
let numbers = [27, 297, 38502, 81];
|
||||||
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
|
// Collects to the expected return type. Returns the first error in the
|
||||||
|
// division results (if one exists).
|
||||||
|
division_results.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_of_results() -> Vec<Result<i64, DivisionError>> {
|
||||||
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
let numbers = [27, 297, 38502, 81];
|
||||||
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
|
// Collects to the expected return type.
|
||||||
|
division_results.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_success() {
|
||||||
|
assert_eq!(divide(81, 9), Ok(9));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_divide_by_0() {
|
||||||
|
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_not_divisible() {
|
||||||
|
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_divide_0_by_something() {
|
||||||
|
assert_eq!(divide(0, 81), Ok(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_result_with_list() {
|
||||||
|
assert_eq!(result_with_list().unwrap(), [1, 11, 1426, 3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_list_of_results() {
|
||||||
|
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,71 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
// 3 possible solutions are presented.
|
||||||
|
|
||||||
|
// With `for` loop and a mutable variable.
|
||||||
|
fn factorial_for(num: u64) -> u64 {
|
||||||
|
let mut result = 1;
|
||||||
|
|
||||||
|
for x in 2..=num {
|
||||||
|
result *= x;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equivalent to `factorial_for` but shorter and without a `for` loop and
|
||||||
|
// mutable variables.
|
||||||
|
fn factorial_fold(num: u64) -> u64 {
|
||||||
|
// Case num==0: The iterator 2..=0 is empty
|
||||||
|
// -> The initial value of `fold` is returned which is 1.
|
||||||
|
// Case num==1: The iterator 2..=1 is also empty
|
||||||
|
// -> The initial value 1 is returned.
|
||||||
|
// Case num==2: The iterator 2..=2 contains one element
|
||||||
|
// -> The initial value 1 is multiplied by 2 and the result
|
||||||
|
// is returned.
|
||||||
|
// Case num==3: The iterator 2..=3 contains 2 elements
|
||||||
|
// -> 1 * 2 is calculated, then the result 2 is multiplied by
|
||||||
|
// the second element 3 so the result 6 is returned.
|
||||||
|
// And so on…
|
||||||
|
(2..=num).fold(1, |acc, x| acc * x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equivalent to `factorial_fold` but with a built-in method that is suggested
|
||||||
|
// by Clippy.
|
||||||
|
fn factorial_product(num: u64) -> u64 {
|
||||||
|
(2..=num).product()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn factorial_of_0() {
|
||||||
|
assert_eq!(factorial_for(0), 1);
|
||||||
|
assert_eq!(factorial_fold(0), 1);
|
||||||
|
assert_eq!(factorial_product(0), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn factorial_of_1() {
|
||||||
|
assert_eq!(factorial_for(1), 1);
|
||||||
|
assert_eq!(factorial_fold(1), 1);
|
||||||
|
assert_eq!(factorial_product(1), 1);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn factorial_of_2() {
|
||||||
|
assert_eq!(factorial_for(2), 2);
|
||||||
|
assert_eq!(factorial_fold(2), 2);
|
||||||
|
assert_eq!(factorial_product(2), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn factorial_of_4() {
|
||||||
|
assert_eq!(factorial_for(4), 24);
|
||||||
|
assert_eq!(factorial_fold(4), 24);
|
||||||
|
assert_eq!(factorial_product(4), 24);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,150 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
// Let's define a simple model to track Rustlings' exercise progress. Progress
|
||||||
|
// will be modelled using a hash map. The name of the exercise is the key and
|
||||||
|
// the progress is the value. Two counting functions were created to count the
|
||||||
|
// number of exercises with a given progress. Recreate this counting
|
||||||
|
// functionality using iterators. Try to not use imperative loops (for/while).
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum Progress {
|
||||||
|
None,
|
||||||
|
Some,
|
||||||
|
Complete,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
|
let mut count = 0;
|
||||||
|
for val in map.values() {
|
||||||
|
if *val == value {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
|
// `map` is a hash map with `String` keys and `Progress` values.
|
||||||
|
// map = { "variables1": Complete, "from_str": None, … }
|
||||||
|
map.values().filter(|val| **val == value).count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||||
|
let mut count = 0;
|
||||||
|
for map in collection {
|
||||||
|
count += count_for(map, value);
|
||||||
|
}
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||||
|
// `collection` is a slice of hash maps.
|
||||||
|
// collection = [{ "variables1": Complete, "from_str": None, … },
|
||||||
|
// { "variables2": Complete, … }, … ]
|
||||||
|
collection
|
||||||
|
.iter()
|
||||||
|
.map(|map| count_iterator(map, value))
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn get_map() -> HashMap<String, Progress> {
|
||||||
|
use Progress::*;
|
||||||
|
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
map.insert(String::from("variables1"), Complete);
|
||||||
|
map.insert(String::from("functions1"), Complete);
|
||||||
|
map.insert(String::from("hashmap1"), Complete);
|
||||||
|
map.insert(String::from("arc1"), Some);
|
||||||
|
map.insert(String::from("as_ref_mut"), None);
|
||||||
|
map.insert(String::from("from_str"), None);
|
||||||
|
|
||||||
|
map
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
|
||||||
|
use Progress::*;
|
||||||
|
|
||||||
|
let map = get_map();
|
||||||
|
|
||||||
|
let mut other = HashMap::new();
|
||||||
|
other.insert(String::from("variables2"), Complete);
|
||||||
|
other.insert(String::from("functions2"), Complete);
|
||||||
|
other.insert(String::from("if1"), Complete);
|
||||||
|
other.insert(String::from("from_into"), None);
|
||||||
|
other.insert(String::from("try_from_into"), None);
|
||||||
|
|
||||||
|
vec![map, other]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_complete() {
|
||||||
|
let map = get_map();
|
||||||
|
assert_eq!(count_iterator(&map, Progress::Complete), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_some() {
|
||||||
|
let map = get_map();
|
||||||
|
assert_eq!(count_iterator(&map, Progress::Some), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_none() {
|
||||||
|
let map = get_map();
|
||||||
|
assert_eq!(count_iterator(&map, Progress::None), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_complete_equals_for() {
|
||||||
|
let map = get_map();
|
||||||
|
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
|
||||||
|
for progress_state in progress_states {
|
||||||
|
assert_eq!(
|
||||||
|
count_for(&map, progress_state),
|
||||||
|
count_iterator(&map, progress_state),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_complete() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
assert_eq!(
|
||||||
|
count_collection_iterator(&collection, Progress::Complete),
|
||||||
|
6,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_some() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
assert_eq!(count_collection_iterator(&collection, Progress::Some), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_none() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
assert_eq!(count_collection_iterator(&collection, Progress::None), 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn count_collection_equals_for() {
|
||||||
|
let collection = get_vec_map();
|
||||||
|
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
|
||||||
|
|
||||||
|
for progress_state in progress_states {
|
||||||
|
assert_eq!(
|
||||||
|
count_collection_for(&collection, progress_state),
|
||||||
|
count_collection_iterator(&collection, progress_state),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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