Skip to content

Commit e3a20b8

Browse files
Learning about vectors
1 parent f7678b4 commit e3a20b8

File tree

5 files changed

+9
-19
lines changed

5 files changed

+9
-19
lines changed

exercises/move_semantics/move_semantics1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// move_semantics1.rs
22
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint.
33

4-
// I AM NOT DONE
5-
64
fn main() {
75
let vec0 = Vec::new();
86

9-
let vec1 = fill_vec(vec0);
7+
let mut vec1 = fill_vec(vec0);
108

119
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
1210

exercises/move_semantics/move_semantics2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// Make me compile without changing line 13 or moving line 10!
33
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
44

5-
// I AM NOT DONE
6-
75
fn main() {
8-
let vec0 = Vec::new();
6+
let mut vec0 = Vec::new();
97

10-
let mut vec1 = fill_vec(vec0);
8+
let mut vec1 = fill_vec(&mut vec0);
119

1210
// Do not change the following line!
1311
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
@@ -17,12 +15,12 @@ fn main() {
1715
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
1816
}
1917

20-
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
18+
fn fill_vec(vec: &mut Vec<i32>) -> Vec<i32> {
2119
let mut vec = vec;
2220

2321
vec.push(22);
2422
vec.push(44);
2523
vec.push(66);
2624

27-
vec
25+
vec.to_vec()
2826
}

exercises/move_semantics/move_semantics3.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// (no lines with multiple semicolons necessary!)
44
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand for a hint.
55

6-
// I AM NOT DONE
7-
86
fn main() {
97
let vec0 = Vec::new();
108

@@ -17,7 +15,7 @@ fn main() {
1715
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
1816
}
1917

20-
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
18+
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
2119
vec.push(22);
2220
vec.push(44);
2321
vec.push(66);

exercises/vecs/vecs1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
// Make me compile and pass the test!
55
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
66

7-
// I AM NOT DONE
8-
97
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
108
let a = [10, 20, 30, 40]; // a plain array
11-
let v = // TODO: declare your vector here with the macro for vectors
9+
let v = vec![10, 20, 30, 40]; // TODO: declare your vector here with the macro for vectors
1210

1311
(a, v)
1412
}

exercises/vecs/vecs2.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
//
77
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
88

9-
// I AM NOT DONE
10-
119
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
1210
for i in v.iter_mut() {
1311
// TODO: Fill this up so that each element in the Vec `v` is
1412
// multiplied by 2.
15-
???
13+
*i *= 2;
1614
}
1715

1816
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
@@ -23,7 +21,7 @@ fn vec_map(v: &Vec<i32>) -> Vec<i32> {
2321
v.iter().map(|num| {
2422
// TODO: Do the same thing as above - but instead of mutating the
2523
// Vec, you can just return the new number!
26-
???
24+
num * 2
2725
}).collect()
2826
}
2927

0 commit comments

Comments
 (0)