mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-29 08:44:16 -05:00
feat(vec): update vec exercises
This commit is contained in:
parent
7af12ba9aa
commit
8e1f617d34
3 changed files with 28 additions and 6 deletions
|
@ -2,7 +2,7 @@
|
||||||
// Your task is to create a `Vec` which holds the exact same elements
|
// Your task is to create a `Vec` which holds the exact same elements
|
||||||
// as in the array `a`.
|
// as in the array `a`.
|
||||||
// Make me compile and pass the test!
|
// 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
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,7 @@
|
||||||
//
|
//
|
||||||
// Make me pass the test!
|
// Make me pass the test!
|
||||||
//
|
//
|
||||||
// Execute the command `rustlings hint vec2` if you need
|
// Execute `rustlings hint vec2` or use the `hint` watch subcommand for a hint.
|
||||||
// hints.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
@ -13,12 +12,21 @@ fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
||||||
for i in v.iter_mut() {
|
for i in v.iter_mut() {
|
||||||
// TODO: Fill this up so that each element in the Vec `v` is
|
// TODO: Fill this up so that each element in the Vec `v` is
|
||||||
// multiplied by 2.
|
// multiplied by 2.
|
||||||
|
???
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
||||||
v
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -30,4 +38,12 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
|
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>>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
12
info.toml
12
info.toml
|
@ -260,9 +260,15 @@ name = "vec2"
|
||||||
path = "exercises/collections/vec2.rs"
|
path = "exercises/collections/vec2.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
hint = """
|
hint = """
|
||||||
Hint 1: `i` is each element from the Vec as they are being iterated.
|
Hint 1: `i` is each element from the Vec as they are being iterated. Can you try
|
||||||
Can you try multiplying this?
|
multiplying this?
|
||||||
Hint 2: Check the suggestion from the compiler error ;)
|
|
||||||
|
Hint 2: For the first function, there's a way to directly access the numbers stored
|
||||||
|
in the Vec, using the * dereference operator. You can both access and write to the
|
||||||
|
number that way.
|
||||||
|
|
||||||
|
After you've completed both functions, decide for yourself which approach you like
|
||||||
|
better. What do you think is the more commonly used pattern under Rust developers?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# MOVE SEMANTICS
|
# MOVE SEMANTICS
|
||||||
|
|
Loading…
Reference in a new issue