···44 // TODO: Create a vector called `v` which contains the exact same elements as in the array `a`.
55 // Use the vector macro.
66 // let v = ???;
77+ let v = vec![10, 20, 30, 40];
7889 (a, v)
910}
+2
exercises/05_vecs/vecs2.rs
···44 for element in input {
55 // TODO: Multiply each element in the `input` slice by 2 and push it to
66 // the `output` vector.
77+ output.push(element * 2)
78 }
89910 output
···2526 .iter()
2627 .map(|element| {
2728 // ???
2929+ element * 2
2830 })
2931 .collect()
3032}
+21-2
solutions/05_vecs/vecs1.rs
···11+fn array_and_vec() -> ([i32; 4], Vec<i32>) {
22+ let a = [10, 20, 30, 40]; // Array
33+44+ // Used the `vec!` macro.
55+ let v = vec![10, 20, 30, 40];
66+77+ (a, v)
88+}
99+110fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
1111+ // You can optionally experiment here.
1212+}
1313+1414+#[cfg(test)]
1515+mod tests {
1616+ use super::*;
1717+1818+ #[test]
1919+ fn test_array_and_vec_similarity() {
2020+ let (a, v) = array_and_vec();
2121+ assert_eq!(a, *v);
2222+ }
423}
+53-2
solutions/05_vecs/vecs2.rs
···11+fn vec_loop(input: &[i32]) -> Vec<i32> {
22+ let mut output = Vec::new();
33+44+ for element in input {
55+ output.push(2 * element);
66+ }
77+88+ output
99+}
1010+1111+fn vec_map_example(input: &[i32]) -> Vec<i32> {
1212+ // An example of collecting a vector after mapping.
1313+ // We map each element of the `input` slice to its value plus 1.
1414+ // If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`.
1515+ input.iter().map(|element| element + 1).collect()
1616+}
1717+1818+fn vec_map(input: &[i32]) -> Vec<i32> {
1919+ // We will dive deeper into iterators, but for now, this is all what you
2020+ // had to do!
2121+ // Advanced note: This method is more efficient because it automatically
2222+ // preallocates enough capacity. This can be done manually in `vec_loop`
2323+ // using `Vec::with_capacity(input.len())` instead of `Vec::new()`.
2424+ input.iter().map(|element| 2 * element).collect()
2525+}
2626+127fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
2828+ // You can optionally experiment here.
2929+}
3030+3131+#[cfg(test)]
3232+mod tests {
3333+ use super::*;
3434+3535+ #[test]
3636+ fn test_vec_loop() {
3737+ let input = [2, 4, 6, 8, 10];
3838+ let ans = vec_loop(&input);
3939+ assert_eq!(ans, [4, 8, 12, 16, 20]);
4040+ }
4141+4242+ #[test]
4343+ fn test_vec_map_example() {
4444+ let input = [1, 2, 3];
4545+ let ans = vec_map_example(&input);
4646+ assert_eq!(ans, [2, 3, 4]);
4747+ }
4848+4949+ #[test]
5050+ fn test_vec_map() {
5151+ let input = [2, 4, 6, 8, 10];
5252+ let ans = vec_map(&input);
5353+ assert_eq!(ans, [4, 8, 12, 16, 20]);
5454+ }
455}