Complete vectors exercise

This commit is contained in:
Suzie Kim 2024-01-31 11:57:41 -05:00
parent d3ae805794
commit 7db46c7e5b
No known key found for this signature in database
GPG key ID: 83C4CC3808F9AAE9
2 changed files with 7 additions and 6 deletions

View file

@ -7,12 +7,12 @@
// //
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn array_and_vec() -> ([i32; 4], Vec<i32>) { fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array let a = [10, 20, 30, 40]; // a plain array
let v = // TODO: declare your vector here with the macro for vectors
// this is the common way to do it using the vector macro
let v = vec![10, 20, 30, 40];// TODO: declare your vector here with the macro for vectors
(a, v) (a, v)
} }

View file

@ -7,13 +7,14 @@
// //
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. // 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> { fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for element in v.iter_mut() { for element 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.
???
*element *= 2;
// the star in front of the element is a dereference operator
// it is used to access the value of the element to change it.
} }
// 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].
@ -24,7 +25,7 @@ fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|element| { v.iter().map(|element| {
// TODO: Do the same thing as above - but instead of mutating the // TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number! // Vec, you can just return the new number!
??? element * 2
}).collect() }).collect()
} }