control flow, primitive types and vectors
This commit is contained in:
@@ -3,7 +3,7 @@ fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
||||
|
||||
// TODO: Create a vector called `v` which contains the exact same elements as in the array `a`.
|
||||
// Use the vector macro.
|
||||
// let v = ???;
|
||||
let v = vec![10, 20, 30, 40];
|
||||
|
||||
(a, v)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
|
||||
for element in input {
|
||||
// TODO: Multiply each element in the `input` slice by 2 and push it to
|
||||
// the `output` vector.
|
||||
output.push(*element * 2);
|
||||
}
|
||||
|
||||
output
|
||||
@@ -21,12 +22,7 @@ fn vec_map(input: &[i32]) -> Vec<i32> {
|
||||
// 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()
|
||||
input.iter().map(|element| element * 2).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user