diff options
Diffstat (limited to 'exercises/05_vecs')
| -rw-r--r-- | exercises/05_vecs/README.md | 17 | ||||
| -rw-r--r-- | exercises/05_vecs/vecs1.rs | 28 | ||||
| -rw-r--r-- | exercises/05_vecs/vecs2.rs | 50 |
3 files changed, 95 insertions, 0 deletions
diff --git a/exercises/05_vecs/README.md b/exercises/05_vecs/README.md new file mode 100644 index 0000000..8ff9b85 --- /dev/null +++ b/exercises/05_vecs/README.md @@ -0,0 +1,17 @@ +# 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) +- [`iter_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.iter_mut) +- [`map`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map) diff --git a/exercises/05_vecs/vecs1.rs b/exercises/05_vecs/vecs1.rs new file mode 100644 index 0000000..65b7a7f --- /dev/null +++ b/exercises/05_vecs/vecs1.rs @@ -0,0 +1,28 @@ +// 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<i32>) { + 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/05_vecs/vecs2.rs b/exercises/05_vecs/vecs2.rs new file mode 100644 index 0000000..e92c970 --- /dev/null +++ b/exercises/05_vecs/vecs2.rs @@ -0,0 +1,50 @@ +// 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<i32>) -> Vec<i32> { + for element 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<i32>) -> Vec<i32> { + v.iter().map(|element| { + // 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<i32> = (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::<Vec<i32>>()); + } + + #[test] + fn test_vec_map() { + let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect(); + let ans = vec_map(&v); + + assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>()); + } +} |
