🦀 Vectors Solved

This commit is contained in:
asif256000 2022-12-23 02:04:04 -05:00
parent bdcac7e4d0
commit 29af744b2b
2 changed files with 9 additions and 11 deletions

View File

@ -4,11 +4,9 @@
// Make me compile and pass the test!
// 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>) {
let a = [10, 20, 30, 40]; // a plain array
let v = // TODO: declare your vector here with the macro for vectors
let v = vec![10, 20, 30, 40]; // TODO: declare your vector here with the macro for vectors
(a, v)
}

View File

@ -6,13 +6,11 @@
//
// 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.
???
*i *= 2;
}
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
@ -20,11 +18,13 @@ fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
}
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()
v.iter()
.map(|num| {
// TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number!
num * 2
})
.collect()
}
#[cfg(test)]