+7
-2
.rustlings-state.txt
+7
-2
.rustlings-state.txt
+1
-1
exercises/06_move_semantics/move_semantics1.rs
+1
-1
exercises/06_move_semantics/move_semantics1.rs
+1
-1
exercises/06_move_semantics/move_semantics2.rs
+1
-1
exercises/06_move_semantics/move_semantics2.rs
+1
-1
exercises/06_move_semantics/move_semantics3.rs
+1
-1
exercises/06_move_semantics/move_semantics3.rs
+1
-1
exercises/06_move_semantics/move_semantics4.rs
+1
-1
exercises/06_move_semantics/move_semantics4.rs
+4
-4
exercises/06_move_semantics/move_semantics5.rs
+4
-4
exercises/06_move_semantics/move_semantics5.rs
···
4
4
// removing references (the character `&`).
5
5
6
6
// Shouldn't take ownership
7
-
fn get_char(data: String) -> char {
7
+
fn get_char(data: &String) -> char {
8
8
data.chars().last().unwrap()
9
9
}
10
10
11
11
// Should take ownership
12
-
fn string_uppercase(mut data: &String) {
12
+
fn string_uppercase(mut data: String) {
13
13
data = data.to_uppercase();
14
14
15
15
println!("{data}");
···
18
18
fn main() {
19
19
let data = "Rust is great!".to_string();
20
20
21
-
get_char(data);
21
+
get_char(&data);
22
22
23
-
string_uppercase(&data);
23
+
string_uppercase(data);
24
24
}
+23
-2
solutions/06_move_semantics/move_semantics1.rs
+23
-2
solutions/06_move_semantics/move_semantics1.rs
···
1
+
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
2
+
let mut vec = vec;
3
+
// ^^^ added
4
+
5
+
vec.push(88);
6
+
7
+
vec
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 move_semantics1() {
20
+
let vec0 = vec![22, 44, 66];
21
+
let vec1 = fill_vec(vec0);
22
+
// `vec0` can't be accessed anymore because it is moved to `fill_vec`.
23
+
assert_eq!(vec1, vec![22, 44, 66, 88]);
24
+
}
4
25
}
+26
-2
solutions/06_move_semantics/move_semantics2.rs
+26
-2
solutions/06_move_semantics/move_semantics2.rs
···
1
+
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
2
+
let mut vec = vec;
3
+
4
+
vec.push(88);
5
+
6
+
vec
7
+
}
8
+
1
9
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
10
+
// You can optionally experiment here.
11
+
}
12
+
13
+
#[cfg(test)]
14
+
mod tests {
15
+
use super::*;
16
+
17
+
#[test]
18
+
fn move_semantics2() {
19
+
let vec0 = vec![22, 44, 66];
20
+
21
+
// Cloning `vec0` so that the clone is moved into `fill_vec`, not `vec0`
22
+
// itself.
23
+
let vec1 = fill_vec(vec0.clone());
24
+
25
+
assert_eq!(vec0, [22, 44, 66]);
26
+
assert_eq!(vec1, [22, 44, 66, 88]);
27
+
}
4
28
}
+20
-2
solutions/06_move_semantics/move_semantics3.rs
+20
-2
solutions/06_move_semantics/move_semantics3.rs
···
1
+
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
2
+
// ^^^ added
3
+
vec.push(88);
4
+
5
+
vec
6
+
}
7
+
1
8
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
9
+
// You can optionally experiment here.
10
+
}
11
+
12
+
#[cfg(test)]
13
+
mod tests {
14
+
use super::*;
15
+
16
+
#[test]
17
+
fn move_semantics3() {
18
+
let vec0 = vec![22, 44, 66];
19
+
let vec1 = fill_vec(vec0);
20
+
assert_eq!(vec1, [22, 44, 66, 88]);
21
+
}
4
22
}
+19
-2
solutions/06_move_semantics/move_semantics4.rs
+19
-2
solutions/06_move_semantics/move_semantics4.rs
···
1
1
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
2
+
// You can optionally experiment here.
3
+
}
4
+
5
+
#[cfg(test)]
6
+
mod tests {
7
+
// TODO: Fix the compiler errors only by reordering the lines in the test.
8
+
// Don't add, change or remove any line.
9
+
#[test]
10
+
fn move_semantics4() {
11
+
let mut x = Vec::new();
12
+
let y = &mut x;
13
+
// `y` used here.
14
+
y.push(42);
15
+
// The mutable reference `y` is not used anymore,
16
+
// therefore a new reference can be created.
17
+
let z = &mut x;
18
+
z.push(13);
19
+
assert_eq!(x, [42, 13]);
20
+
}
4
21
}
+21
-2
solutions/06_move_semantics/move_semantics5.rs
+21
-2
solutions/06_move_semantics/move_semantics5.rs
···
1
+
#![allow(clippy::ptr_arg)]
2
+
3
+
// Borrows instead of taking ownership.
4
+
// It is recommended to use `&str` instead of `&String` here. But this is
5
+
// enough for now because we didn't handle strings yet.
6
+
fn get_char(data: &String) -> char {
7
+
data.chars().last().unwrap()
8
+
}
9
+
10
+
// Takes ownership instead of borrowing.
11
+
fn string_uppercase(mut data: String) {
12
+
data = data.to_uppercase();
13
+
14
+
println!("{data}");
15
+
}
16
+
1
17
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
18
+
let data = "Rust is great!".to_string();
19
+
20
+
get_char(&data);
21
+
22
+
string_uppercase(data);
4
23
}