summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authormokou <mokou@fastmail.com>2022-07-12 15:05:47 +0200
committermokou <mokou@fastmail.com>2022-07-12 15:05:47 +0200
commit8e1f617d3402cb05b05c6737f60fbbfe74da4d78 (patch)
treec771ea375a82d6e3dd65f28fad3978c7b7aacfbe /exercises
parent7af12ba9aa45b57730b92cc00c3ddaa1527eb31f (diff)
feat(vec): update vec exercises
Diffstat (limited to 'exercises')
-rw-r--r--exercises/collections/vec1.rs2
-rw-r--r--exercises/collections/vec2.rs20
2 files changed, 19 insertions, 3 deletions
diff --git a/exercises/collections/vec1.rs b/exercises/collections/vec1.rs
index b144fb9..c26f569 100644
--- a/exercises/collections/vec1.rs
+++ b/exercises/collections/vec1.rs
@@ -2,7 +2,7 @@
// 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 vec1` if you need hints.
+// Execute `rustlings hint vec1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
diff --git a/exercises/collections/vec2.rs b/exercises/collections/vec2.rs
index 6595e40..db37d1d 100644
--- a/exercises/collections/vec2.rs
+++ b/exercises/collections/vec2.rs
@@ -4,8 +4,7 @@
//
// Make me pass the test!
//
-// Execute the command `rustlings hint vec2` if you need
-// hints.
+// Execute `rustlings hint vec2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
@@ -13,12 +12,21 @@ fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
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<i32>) -> Vec<i32> {
+ 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::*;
@@ -30,4 +38,12 @@ mod tests {
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>>());
+ }
}