mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-22 00:18:09 -05:00
vecs2 solution + significant change to have a better comparison between both methods
This commit is contained in:
parent
a9f0c7bf1f
commit
835ec72622
3 changed files with 89 additions and 33 deletions
|
@ -1,25 +1,32 @@
|
|||
// 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!
|
||||
fn vec_loop(input: &[i32]) -> Vec<i32> {
|
||||
let mut output = Vec::new();
|
||||
|
||||
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.
|
||||
???
|
||||
for element in input {
|
||||
// TODO: Multiply each element in the `input` slice by 2 and push it to
|
||||
// the `output` vector.
|
||||
}
|
||||
|
||||
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
||||
v
|
||||
output
|
||||
}
|
||||
|
||||
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()
|
||||
fn vec_map_example(input: &[i32]) -> Vec<i32> {
|
||||
// An example of collecting a vector after mapping.
|
||||
// We map each element of the `input` slice to its value plus 1.
|
||||
// If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`.
|
||||
input.iter().map(|element| element + 1).collect()
|
||||
}
|
||||
|
||||
fn vec_map(input: &[i32]) -> Vec<i32> {
|
||||
// TODO: Here, we also want to multiply each element in the `input` slice
|
||||
// by 2, but with iterator mapping instead of manually pushing into an empty
|
||||
// vector.
|
||||
// See the example in the function `vec_map_example` above.
|
||||
input
|
||||
.iter()
|
||||
.map(|element| {
|
||||
// ???
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -32,17 +39,22 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_vec_loop() {
|
||||
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
||||
let ans = vec_loop(v.clone());
|
||||
let input = [2, 4, 6, 8, 10];
|
||||
let ans = vec_loop(&input);
|
||||
assert_eq!(ans, [4, 8, 12, 16, 20]);
|
||||
}
|
||||
|
||||
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
|
||||
#[test]
|
||||
fn test_vec_map_example() {
|
||||
let input = [1, 2, 3];
|
||||
let ans = vec_map_example(&input);
|
||||
assert_eq!(ans, [2, 3, 4]);
|
||||
}
|
||||
|
||||
#[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>>());
|
||||
let input = [2, 4, 6, 8, 10];
|
||||
let ans = vec_map(&input);
|
||||
assert_eq!(ans, [4, 8, 12, 16, 20]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -319,15 +319,10 @@ of the Rust book to learn more.
|
|||
name = "vecs2"
|
||||
dir = "05_vecs"
|
||||
hint = """
|
||||
In the first function we are looping over the Vector and getting a reference to
|
||||
one `element` at a time.
|
||||
In the first function, we create an empty vector and want to push new elements
|
||||
to it.
|
||||
|
||||
To modify the value of that `element` we need to use the `*` dereference
|
||||
operator. You can learn more in this chapter of the Rust book:
|
||||
https://doc.rust-lang.org/stable/book/ch08-01-vectors.html#iterating-over-the-values-in-a-vector
|
||||
|
||||
In the second function this dereferencing is not necessary, because the `map`
|
||||
function expects the new value to be returned.
|
||||
In the second function, we map the values of the input and collect them into a vector.
|
||||
|
||||
After you've completed both functions, decide for yourself which approach you
|
||||
like better.
|
||||
|
|
|
@ -1 +1,50 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
fn vec_loop(input: &[i32]) -> Vec<i32> {
|
||||
let mut output = Vec::new();
|
||||
|
||||
for element in input {
|
||||
output.push(2 * element);
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn vec_map_example(input: &[i32]) -> Vec<i32> {
|
||||
// An example of collecting a vector after mapping.
|
||||
// We map each element of the `input` slice to its value plus 1.
|
||||
// If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`.
|
||||
input.iter().map(|element| element + 1).collect()
|
||||
}
|
||||
|
||||
fn vec_map(input: &[i32]) -> Vec<i32> {
|
||||
input.iter().map(|element| 2 * element).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_vec_loop() {
|
||||
let input = [2, 4, 6, 8, 10];
|
||||
let ans = vec_loop(&input);
|
||||
assert_eq!(ans, [4, 8, 12, 16, 20]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_map_example() {
|
||||
let input = [1, 2, 3];
|
||||
let ans = vec_map_example(&input);
|
||||
assert_eq!(ans, [2, 3, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_map() {
|
||||
let input = [2, 4, 6, 8, 10];
|
||||
let ans = vec_map(&input);
|
||||
assert_eq!(ans, [4, 8, 12, 16, 20]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue