Going through rustlings for the first time

ex(05): vecs

+81 -6
+4 -2
.rustlings-state.txt
··· 1 1 DON'T EDIT THIS FILE! 2 2 3 - vecs1 3 + move_semantics1 4 4 5 5 intro1 6 6 intro2 ··· 24 24 primitive_types3 25 25 primitive_types4 26 26 primitive_types5 27 - primitive_types6 27 + primitive_types6 28 + vecs1 29 + vecs2
+1
exercises/05_vecs/vecs1.rs
··· 4 4 // TODO: Create a vector called `v` which contains the exact same elements as in the array `a`. 5 5 // Use the vector macro. 6 6 // let v = ???; 7 + let v = vec![10, 20, 30, 40]; 7 8 8 9 (a, v) 9 10 }
+2
exercises/05_vecs/vecs2.rs
··· 4 4 for element in input { 5 5 // TODO: Multiply each element in the `input` slice by 2 and push it to 6 6 // the `output` vector. 7 + output.push(element * 2) 7 8 } 8 9 9 10 output ··· 25 26 .iter() 26 27 .map(|element| { 27 28 // ??? 29 + element * 2 28 30 }) 29 31 .collect() 30 32 }
+21 -2
solutions/05_vecs/vecs1.rs
··· 1 + fn array_and_vec() -> ([i32; 4], Vec<i32>) { 2 + let a = [10, 20, 30, 40]; // Array 3 + 4 + // Used the `vec!` macro. 5 + let v = vec![10, 20, 30, 40]; 6 + 7 + (a, v) 8 + } 9 + 1 10 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 11 + // You can optionally experiment here. 12 + } 13 + 14 + #[cfg(test)] 15 + mod tests { 16 + use super::*; 17 + 18 + #[test] 19 + fn test_array_and_vec_similarity() { 20 + let (a, v) = array_and_vec(); 21 + assert_eq!(a, *v); 22 + } 4 23 }
+53 -2
solutions/05_vecs/vecs2.rs
··· 1 + fn vec_loop(input: &[i32]) -> Vec<i32> { 2 + let mut output = Vec::new(); 3 + 4 + for element in input { 5 + output.push(2 * element); 6 + } 7 + 8 + output 9 + } 10 + 11 + fn vec_map_example(input: &[i32]) -> Vec<i32> { 12 + // An example of collecting a vector after mapping. 13 + // We map each element of the `input` slice to its value plus 1. 14 + // If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`. 15 + input.iter().map(|element| element + 1).collect() 16 + } 17 + 18 + fn vec_map(input: &[i32]) -> Vec<i32> { 19 + // We will dive deeper into iterators, but for now, this is all what you 20 + // had to do! 21 + // Advanced note: This method is more efficient because it automatically 22 + // preallocates enough capacity. This can be done manually in `vec_loop` 23 + // using `Vec::with_capacity(input.len())` instead of `Vec::new()`. 24 + input.iter().map(|element| 2 * element).collect() 25 + } 26 + 1 27 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 28 + // You can optionally experiment here. 29 + } 30 + 31 + #[cfg(test)] 32 + mod tests { 33 + use super::*; 34 + 35 + #[test] 36 + fn test_vec_loop() { 37 + let input = [2, 4, 6, 8, 10]; 38 + let ans = vec_loop(&input); 39 + assert_eq!(ans, [4, 8, 12, 16, 20]); 40 + } 41 + 42 + #[test] 43 + fn test_vec_map_example() { 44 + let input = [1, 2, 3]; 45 + let ans = vec_map_example(&input); 46 + assert_eq!(ans, [2, 3, 4]); 47 + } 48 + 49 + #[test] 50 + fn test_vec_map() { 51 + let input = [2, 4, 6, 8, 10]; 52 + let ans = vec_map(&input); 53 + assert_eq!(ans, [4, 8, 12, 16, 20]); 54 + } 4 55 }