summaryrefslogtreecommitdiff
path: root/exercises/vecs/vecs2.rs
diff options
context:
space:
mode:
authormokou <mokou@fastmail.com>2022-07-12 15:16:25 +0200
committermokou <mokou@fastmail.com>2022-07-12 15:16:25 +0200
commit2f7fd513041c7c6275552650881a79b9120aaacf (patch)
tree0ce4f6afca30d729b1c23081004dc10aed28d32f /exercises/vecs/vecs2.rs
parent8e1f617d3402cb05b05c6737f60fbbfe74da4d78 (diff)
feat: move vec exercises into their own folder
Diffstat (limited to 'exercises/vecs/vecs2.rs')
-rw-r--r--exercises/vecs/vecs2.rs49
1 files changed, 49 insertions, 0 deletions
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<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::*;
+
+ #[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>>());
+ }
+}