From 2f7fd513041c7c6275552650881a79b9120aaacf Mon Sep 17 00:00:00 2001 From: mokou Date: Tue, 12 Jul 2022 15:16:25 +0200 Subject: feat: move vec exercises into their own folder --- exercises/collections/README.md | 24 +++++--------------- exercises/collections/vec1.rs | 25 --------------------- exercises/collections/vec2.rs | 49 ----------------------------------------- exercises/vecs/README.md | 15 +++++++++++++ exercises/vecs/vecs1.rs | 25 +++++++++++++++++++++ exercises/vecs/vecs2.rs | 49 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 92 deletions(-) delete mode 100644 exercises/collections/vec1.rs delete mode 100644 exercises/collections/vec2.rs create mode 100644 exercises/vecs/README.md create mode 100644 exercises/vecs/vecs1.rs create mode 100644 exercises/vecs/vecs2.rs (limited to 'exercises') diff --git a/exercises/collections/README.md b/exercises/collections/README.md index b6d62ac..30471cf 100644 --- a/exercises/collections/README.md +++ b/exercises/collections/README.md @@ -1,23 +1,11 @@ -# Collections +# Hashmaps +A *hash map* allows you to associate a value with a particular key. +You may also know this by the names [*unordered map* in C++](https://en.cppreference.com/w/cpp/container/unordered_map), +[*dictionary* in Python](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) or an *associative array* in other languages. -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 [*unordered map* in C++](https://en.cppreference.com/w/cpp/container/unordered_map), - [*dictionary* in Python](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) or an *associative array* in other languages. +This is the other data structure that we've been talking about before, when +talking about Vecs. ## Further information -- [Storing Lists of Values with Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html) - [Storing Keys with Associated Values in Hash Maps](https://doc.rust-lang.org/book/ch08-03-hash-maps.html) diff --git a/exercises/collections/vec1.rs b/exercises/collections/vec1.rs deleted file mode 100644 index c26f569..0000000 --- a/exercises/collections/vec1.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 `rustlings hint vec1` or use the `hint` watch subcommand for a hint. - -// 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_eq!(a, v[..]); - } -} diff --git a/exercises/collections/vec2.rs b/exercises/collections/vec2.rs deleted file mode 100644 index db37d1d..0000000 --- a/exercises/collections/vec2.rs +++ /dev/null @@ -1,49 +0,0 @@ -// 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 `rustlings hint vec2` or use the `hint` watch subcommand for a hint. - -// 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 -} - -fn vec_map(v: &Vec) -> Vec { - v.iter().map(|num| { - // TODO: Do the same thing as above - but instead of mutating the - // Vec, you can just return the new number! - ??? - }).collect() -} - -#[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::>()); - } - - #[test] - fn test_vec_map() { - let v: Vec = (1..).filter(|x| x % 2 == 0).take(5).collect(); - let ans = vec_map(&v); - - assert_eq!(ans, v.iter().map(|x| x * 2).collect::>()); - } -} diff --git a/exercises/vecs/README.md b/exercises/vecs/README.md new file mode 100644 index 0000000..ebe90bf --- /dev/null +++ b/exercises/vecs/README.md @@ -0,0 +1,15 @@ +# Vectors + +Vectors are one of the most-used Rust data structures. In other programming +languages, they'd simply be called Arrays, but since Rust operates on a +bit of a lower level, an array in Rust is stored on the stack (meaning it +can't grow or shrink, and the size needs to be known at compile time), +and a Vector is stored in the heap (where these restrictions do not apply). + +Vectors are a bit of a later chapter in the book, but we think that they're +useful enough to talk about them a bit earlier. We shall be talking about +the other useful data structure, hash maps, later. + +## Further information + +- [Storing Lists of Values with Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html) diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs new file mode 100644 index 0000000..4e8c4cb --- /dev/null +++ b/exercises/vecs/vecs1.rs @@ -0,0 +1,25 @@ +// vecs1.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 `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. + +// 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_eq!(a, v[..]); + } +} diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs new file mode 100644 index 0000000..5bea09a --- /dev/null +++ b/exercises/vecs/vecs2.rs @@ -0,0 +1,49 @@ +// vecs2.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 `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. + +// 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 +} + +fn vec_map(v: &Vec) -> Vec { + v.iter().map(|num| { + // TODO: Do the same thing as above - but instead of mutating the + // Vec, you can just return the new number! + ??? + }).collect() +} + +#[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::>()); + } + + #[test] + fn test_vec_map() { + let v: Vec = (1..).filter(|x| x % 2 == 0).take(5).collect(); + let ans = vec_map(&v); + + assert_eq!(ans, v.iter().map(|x| x * 2).collect::>()); + } +} -- cgit v1.2.3