move_semantics done

This commit is contained in:
Chris Girvin 2022-05-16 16:56:15 -04:00
parent 34d15b05d8
commit df3743c844
3 changed files with 80 additions and 13 deletions

View File

@ -4,12 +4,35 @@
// freshly created vector from fill_vec to its caller. // freshly created vector from fill_vec to its caller.
// Execute `rustlings hint move_semantics4` for hints! // Execute `rustlings hint move_semantics4` for hints!
// I AM NOT DONE // // original code
// fn main() {
// let vec0 = Vec::new();
// let mut vec1 = fill_vec(vec0);
// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
// vec1.push(88);
// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
// }
// // original code (cont'd)
// // `fill_vec()` no longer takes `vec: Vec<i32>` as argument
// fn fill_vec() -> Vec<i32> {
// let mut vec = vec;
// vec.push(22);
// vec.push(44);
// vec.push(66);
// vec
// }
// // end of original code
// solution
fn main() { fn main() {
let vec0 = Vec::new(); let mut vec1 = fill_vec();
let mut vec1 = fill_vec(vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@ -18,9 +41,11 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
} }
// solution (cont'd)
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument // `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> { fn fill_vec() -> Vec<i32> {
let mut vec = vec; // create mutable vec
let mut vec = Vec::new();
vec.push(22); vec.push(22);
vec.push(44); vec.push(44);
@ -28,3 +53,4 @@ fn fill_vec() -> Vec<i32> {
vec vec
} }
// end of solution

View File

@ -3,13 +3,23 @@
// adding, changing or removing any of them. // adding, changing or removing any of them.
// Execute `rustlings hint move_semantics5` for hints :) // Execute `rustlings hint move_semantics5` for hints :)
// I AM NOT DONE // // original code
// fn main() {
// let mut x = 100;
// let y = &mut x;
// let z = &mut x;
// *y += 100;
// *z += 1000;
// assert_eq!(x, 1200);
// }
// // end of orignal code
// solution
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

@ -2,24 +2,55 @@
// Make me compile! `rustlings hint move_semantics6` for hints // Make me compile! `rustlings hint move_semantics6` for hints
// You can't change anything except adding or removing references // You can't change anything except adding or removing references
// I AM NOT DONE // // original code
// fn main() {
// let data = "Rust is great!".to_string();
// get_char(data);
// string_uppercase(&data);
// }
// // original code (cont'd)
// // Should not take ownership
// fn get_char(data: String) -> char {
// data.chars().last().unwrap()
// }
// // original code (cont'd)
// // Should take ownership
// fn string_uppercase(mut data: &String) {
// data = &data.to_uppercase();
// println!("{}", data);
// }
// // end of original code
// solution
fn main() { fn main() {
let data = "Rust is great!".to_string(); let data = "Rust is great!".to_string();
get_char(data); // change passed data to ref so as to not take ownership of `data`
get_char(&data);
string_uppercase(&data); // remove ref so string_uppercase takes ownership of `data`
string_uppercase(data);
} }
// solution (cont'd)
// Should not take ownership // Should not take ownership
fn get_char(data: String) -> char { // add ref to argument type
fn get_char(data: &String) -> char {
data.chars().last().unwrap() data.chars().last().unwrap()
} }
// solution (cont'd)
// Should take ownership // Should take ownership
fn string_uppercase(mut data: &String) { // remove ref from argument type
data = &data.to_uppercase(); fn string_uppercase(mut data: String) {
// remove ref from data
data = data.to_uppercase();
println!("{}", data); println!("{}", data);
} }
// end of solution