This commit is contained in:
Cody 2023-09-29 12:34:35 +08:00
parent 049ab5e619
commit c67c4b7c50
No known key found for this signature in database
GPG Key ID: C7228F849611151F
7 changed files with 22 additions and 19 deletions

View File

@ -3,7 +3,7 @@
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
// for a hint.
// I AM NOT DONE
// I AM DONE
#[test]
fn main() {
@ -15,9 +15,10 @@ fn main() {
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec;
// 可变引用
let mut vec = vec;
vec.push(88);
vec.push(88);// push需要改变原变量
vec
}

View File

@ -5,13 +5,13 @@
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
// for a hint.
// I AM NOT DONE
// I AM DONE
#[test]
fn main() {
let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0.clone());
assert_eq!(vec0, vec![22, 44, 66]);
assert_eq!(vec1, vec![22, 44, 66, 88]);

View File

@ -6,7 +6,7 @@
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
// for a hint.
// I AM NOT DONE
// I AM DONE
#[test]
fn main() {
@ -17,7 +17,7 @@ fn main() {
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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@
//
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
// I AM DONE
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for element in v.iter_mut() {