move semantics done

This commit is contained in:
Simon Halimi 2021-04-13 15:03:46 +02:00
parent caac1b3bfa
commit 0b66b2945a
4 changed files with 9 additions and 17 deletions

View file

@ -1,12 +1,10 @@
// move_semantics1.rs
// Make me compile! Execute `rustlings hint move_semantics1` for hints :)
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

View file

@ -2,12 +2,12 @@
// Make me compile without changing line 13!
// Execute `rustlings hint move_semantics2` for hints :)
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let vec0 = Vec::<i32>::new();
let vec2 = Vec::<i32>::new();
let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec2);
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);

View file

@ -3,10 +3,8 @@
// (no lines with multiple semicolons necessary!)
// Execute `rustlings hint move_semantics3` for hints :)
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let mut vec0 = Vec::<i32>::new();
let mut vec1 = fill_vec(vec0);
@ -17,7 +15,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
fn fill_vec( mut vec: Vec<i32>) -> Vec<i32> {
vec.push(22);
vec.push(44);
vec.push(66);

View file

@ -2,12 +2,8 @@
// Refactor this code so that instead of having `vec0` and creating the vector
// in `fn main`, we create it within `fn fill_vec` and transfer the
// freshly created vector from fill_vec to its caller.
// Execute `rustlings hint move_semantics4` for hints!
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let vec0 = Vec::<i32>::new();
let mut vec1 = fill_vec(vec0);
@ -19,7 +15,7 @@ fn main() {
}
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
vec.push(22);