Finish move_semantics

This commit is contained in:
Evie Litherland-Smith 2023-12-23 10:31:31 +00:00
parent 82055482e9
commit df0605736a
4 changed files with 10 additions and 24 deletions

View file

@ -6,18 +6,16 @@
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let vec0 = vec![22, 44, 66]; let mut vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0); let mut vec1 = fill_vec(vec0);
assert_eq!(vec1, vec![22, 44, 66, 88]); assert_eq!(vec1, vec![22, 44, 66, 88]);
} }
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88); vec.push(88);
vec vec

View file

@ -7,13 +7,9 @@
// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let vec0 = vec![22, 44, 66]; let mut vec1 = fill_vec();
let mut vec1 = fill_vec(vec0);
assert_eq!(vec1, vec![22, 44, 66, 88]); assert_eq!(vec1, vec![22, 44, 66, 88]);
} }
@ -21,9 +17,5 @@ fn main() {
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this! // `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
fn fill_vec() -> Vec<i32> { fn fill_vec() -> Vec<i32> {
// Instead, let's create and fill the Vec in here - how do you do that? // Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec; vec![22, 44, 66, 88]
vec.push(88);
vec
} }

View file

@ -6,14 +6,12 @@
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let mut x = 100; let mut x = 100;
let y = &mut x; let y = &mut x;
let z = &mut x;
*y += 100; *y += 100;
let z = &mut x;
*z += 1000; *z += 1000;
assert_eq!(x, 1200); assert_eq!(x, 1200);
} }

View file

@ -5,24 +5,22 @@
// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
fn main() { fn main() {
let data = "Rust is great!".to_string(); let data = "Rust is great!".to_string();
get_char(data); get_char(&data);
string_uppercase(&data); string_uppercase(data);
} }
// Should not take ownership // Should not take ownership
fn get_char(data: String) -> char { fn get_char(data: &String) -> char {
data.chars().last().unwrap() data.chars().last().unwrap()
} }
// Should take ownership // Should take ownership
fn string_uppercase(mut data: &String) { fn string_uppercase(mut data: String) {
data = &data.to_uppercase(); data = data.to_uppercase();
println!("{}", data); println!("{}", data);
} }