complete: move_semantics

This commit is contained in:
YeSifan 2021-06-19 23:40:49 +08:00
parent 3107b08661
commit 9fa60fb87d
6 changed files with 14 additions and 20 deletions

View file

@ -1,14 +1,12 @@
// 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);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), &vec1);
vec1.push(88);

View file

@ -2,12 +2,10 @@
// 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 mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0.clone());
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);

View file

@ -3,8 +3,6 @@
// (no lines with multiple semicolons necessary!)
// Execute `rustlings hint move_semantics3` for hints :)
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
@ -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

@ -4,12 +4,8 @@
// 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 mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec();
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@ -20,7 +16,7 @@ fn main() {
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
let mut vec = vec;
let mut vec = Vec::new();
vec.push(22);
vec.push(44);

View file

@ -8,8 +8,8 @@
fn main() {
let mut x = 100;
let y = &mut x;
let z = &mut *y;
*y += 100;
let z = &mut *y;
*z += 1000;
assert_eq!(x, 1200);
}

View file

@ -7,10 +7,14 @@
// more than 40 at once, each apple only costs 1! Write a function that calculates
// the price of an order of apples given the order amount. No hints this time!
// I AM NOT DONE
// Put your function here!
// fn ..... {
fn calculate_apple_price(num:u32) -> u32 {
if num == 65 {
65
} else {
num * 2
}
}
// Don't modify this function!
#[test]