diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index aac6dfc3..f6d5beda 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,12 +1,10 @@ // move_semantics1.rs // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); - let vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); @@ -16,11 +14,9 @@ fn main() { } fn fill_vec(vec: Vec) -> Vec { - let mut vec = vec; - - vec.push(22); - vec.push(44); - vec.push(66); - - vec + let mut vec_x = vec; + vec_x.push(22); + vec_x.push(44); + vec_x.push(66); + vec_x } diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs index 4e8c4cbb..5fb0b7a1 100644 --- a/exercises/vecs/vecs1.rs +++ b/exercises/vecs/vecs1.rs @@ -4,12 +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) { 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]; (a, v) } diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs index 5bea09a2..3384b0b7 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -6,25 +6,24 @@ // // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE -fn vec_loop(mut v: Vec) -> Vec { +fn vec_loop(v: &mut Vec){ for i in v.iter_mut() { // TODO: Fill this up so that each element in the Vec `v` is // multiplied by 2. - ??? + *i = *i * 2 } - // At this point, `v` should be equal to [4, 8, 12, 16, 20]. - v } fn vec_map(v: &Vec) -> Vec { - 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)] @@ -33,10 +32,11 @@ mod tests { #[test] fn test_vec_loop() { - let v: Vec = (1..).filter(|x| x % 2 == 0).take(5).collect(); - let ans = vec_loop(v.clone()); + let a: Vec = (1..).filter(|x| x % 2 == 0).take(5).collect(); + let mut v: Vec = (1..).filter(|x| x % 2 == 0).take(5).collect(); + vec_loop(&mut v); - assert_eq!(ans, v.iter().map(|x| x * 2).collect::>()); + assert_eq!(v, a.iter().map(|x| x * 2).collect::>()); } #[test]