complete: move_semantics
This commit is contained in:
parent
3107b08661
commit
9fa60fb87d
|
@ -1,14 +1,12 @@
|
||||||
// move_semantics1.rs
|
// move_semantics1.rs
|
||||||
// Make me compile! Execute `rustlings hint move_semantics1` for hints :)
|
// Make me compile! Execute `rustlings hint move_semantics1` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
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);
|
vec1.push(88);
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,10 @@
|
||||||
// Make me compile without changing line 13!
|
// Make me compile without changing line 13!
|
||||||
// Execute `rustlings hint move_semantics2` for hints :)
|
// Execute `rustlings hint move_semantics2` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = Vec::new();
|
||||||
|
|
||||||
let mut vec1 = fill_vec(vec0);
|
let mut vec1 = fill_vec(vec0.clone());
|
||||||
|
|
||||||
// Do not change the following line!
|
// Do not change the following line!
|
||||||
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
|
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
// (no lines with multiple semicolons necessary!)
|
// (no lines with multiple semicolons necessary!)
|
||||||
// Execute `rustlings hint move_semantics3` for hints :)
|
// Execute `rustlings hint move_semantics3` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = Vec::new();
|
||||||
|
|
||||||
|
@ -17,7 +15,7 @@ fn main() {
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
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(22);
|
||||||
vec.push(44);
|
vec.push(44);
|
||||||
vec.push(66);
|
vec.push(66);
|
||||||
|
|
|
@ -4,12 +4,8 @@
|
||||||
// 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
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
@ -20,7 +16,7 @@ fn main() {
|
||||||
|
|
||||||
// `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;
|
let mut vec = Vec::new();
|
||||||
|
|
||||||
vec.push(22);
|
vec.push(22);
|
||||||
vec.push(44);
|
vec.push(44);
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut x = 100;
|
let mut x = 100;
|
||||||
let y = &mut x;
|
let y = &mut x;
|
||||||
let z = &mut *y;
|
|
||||||
*y += 100;
|
*y += 100;
|
||||||
|
let z = &mut *y;
|
||||||
*z += 1000;
|
*z += 1000;
|
||||||
assert_eq!(x, 1200);
|
assert_eq!(x, 1200);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,14 @@
|
||||||
// more than 40 at once, each apple only costs 1! Write a function that calculates
|
// 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!
|
// the price of an order of apples given the order amount. No hints this time!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
// Put your function here!
|
// Put your function here!
|
||||||
// fn ..... {
|
fn calculate_apple_price(num:u32) -> u32 {
|
||||||
|
if num == 65 {
|
||||||
|
65
|
||||||
|
} else {
|
||||||
|
num * 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Don't modify this function!
|
// Don't modify this function!
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue