mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 05:09:19 +00:00
vec done
This commit is contained in:
parent
8eb8525699
commit
14d63e2b8e
@ -4,11 +4,10 @@
|
|||||||
// 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 the command `rustlings hint vec1` if you need hints.
|
||||||
|
|
||||||
// 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
|
let v = vec![10,20,30,40];// TODO: declare your vector here with the macro for vectors
|
||||||
|
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,10 +7,13 @@
|
|||||||
// Execute the command `rustlings hint vec2` if you need
|
// Execute the command `rustlings hint vec2` if you need
|
||||||
// hints.
|
// hints.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
||||||
for i in v.iter_mut() {
|
for i in v.iter_mut() {
|
||||||
|
println!("{}",i);
|
||||||
|
*i=*i*2;
|
||||||
|
println!("{}",i);
|
||||||
|
|
||||||
// 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.
|
||||||
}
|
}
|
||||||
@ -26,8 +29,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_vec_loop() {
|
fn test_vec_loop() {
|
||||||
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
||||||
|
let v2:Vec<i32>=(1..13).collect();
|
||||||
let ans = vec_loop(v.clone());
|
let ans = vec_loop(v.clone());
|
||||||
|
|
||||||
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
|
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user