diff --git a/exercises/collections/README.md b/exercises/collections/README.md new file mode 100644 index 0000000000..9ded29a04c --- /dev/null +++ b/exercises/collections/README.md @@ -0,0 +1,20 @@ +### Collections + +Rust’s standard library includes a number of very useful data +structures called collections. Most other data types represent one +specific value, but collections can contain multiple values. Unlike +the built-in array and tuple types, the data these collections point +to is stored on the heap, which means the amount of data does not need +to be known at compile time and can grow or shrink as the program +runs. + +This exercise will get you familiar with two fundamental data +structures that are used very often in Rust programs: + +* A *vector* allows you to store a variable number of values next to + each other. +* A *hash map* allows you to associate a value with a particular key. + You may also know this by the names *map* in C++, *dictionary* in + Python or an *associative array* in other languages. + +[Rust book chapter](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html) diff --git a/exercises/collections/vec1.rs b/exercises/collections/vec1.rs new file mode 100644 index 0000000000..ac3d9f1acb --- /dev/null +++ b/exercises/collections/vec1.rs @@ -0,0 +1,25 @@ +// vec1.rs +// Your task is to create a `Vec` which holds the exact same elements +// as in the array `a`. +// Make me compile and pass the test! +// Execute the command `rustlings hint collections1` if you need hints. + +// I AM NOT DONE + +fn array_and_vec() -> ([i32; 4], Vec) { + let a = [10, 20, 30, 40]; // a plain array + let v = // TODO: declare your vector here with the macro for vectors + + (a, v) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_array_and_vec_similarity() { + let (a, v) = array_and_vec(); + assert!(a.iter().zip(v.iter()).all(|(x, y)| x == y)); + } +} diff --git a/exercises/collections/vec2.rs b/exercises/collections/vec2.rs new file mode 100644 index 0000000000..ec6cfc004b --- /dev/null +++ b/exercises/collections/vec2.rs @@ -0,0 +1,38 @@ +// vec2.rs +// A Vec of even numbers is given. Your task is to complete the loop +// so that each number in the Vec is multiplied by 2. +// +// Make me pass the test! +// +// Execute the command `rustlings hint collections2` if you need +// hints. + +// I AM NOT DONE + +fn vec_loop(mut v: Vec) -> Vec { + for i in v.iter_mut() { + // TODO: Fill this up so that each element in the Vec `v` is + // multiplied by 2. + } + + // At this point, `v` should be equal to [4, 8, 12, 16, 20]. + v +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_vec_loop() { + let v: Vec = (1..).filter(|x| x % 2 == 0).take(5).collect(); + let ans = vec_loop(v.clone()); + + assert_eq!( + ans, + v.iter() + .map(|x| x * 2) + .collect::>() + ); + } +} diff --git a/info.toml b/info.toml index 2d0abdb15a..56605a7b73 100644 --- a/info.toml +++ b/info.toml @@ -370,6 +370,36 @@ its internal structure (the `fruits` and `veggies` modules and associated constants). It's almost there except for one keyword missing for each constant.""" +# COLLECTIONS + +[[exercises]] +name = "collections1" +path = "exercises/collections/vec1.rs" +mode = "test" +hint = """ +In Rust, there are two ways to define a Vector. + +1. One way is to use the `Vec::new()` function to create a new vector + and fill it with the `push()` method. + +2. The second way, which is simpler is to use the `vec![]` macro and + define your elements inside the square brackets. + +Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html +of the Rust book to learn more. +""" + +[[exercises]] +name = "collections2" +path = "exercises/collections/vec2.rs" +mode = "test" +hint = """ +Hint 1: `i` is each element from the Vec as they are being iterated. + Can you try multiplying this? + +Hint 2: Check the suggestion from the compiler error ;) +""" + # MACROS [[exercises]]