From 76d54ad0a7f30c9df7a3f10a0165473ae57b9dd3 Mon Sep 17 00:00:00 2001 From: why <435371447@qq.com> Date: Mon, 8 Apr 2024 09:34:13 +0800 Subject: [PATCH] update --- exercises/move_semantics/move_semantics1.rs | 4 ++-- exercises/move_semantics/move_semantics2.rs | 4 ++-- exercises/move_semantics/move_semantics3.rs | 3 ++- exercises/move_semantics/move_semantics4.rs | 4 ++-- exercises/move_semantics/move_semantics5.rs | 4 ++-- exercises/move_semantics/move_semantics6.rs | 12 ++++++------ exercises/vecs/vecs1.rs | 4 ++-- exercises/vecs/vecs2.rs | 6 +++--- 8 files changed, 21 insertions(+), 20 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index 710d20d..67d689d 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -3,12 +3,12 @@ // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand // for a hint. -// 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); diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs index 72d37fa..bc0425c 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -7,12 +7,12 @@ // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE + fn main() { let vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0.clone()); println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0); diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index ea21493..c036ffb 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -6,7 +6,7 @@ // Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE + fn main() { let vec0 = Vec::new(); @@ -21,6 +21,7 @@ fn main() { } fn fill_vec(vec: Vec) -> Vec { + let mut vec = vec; vec.push(22); vec.push(44); vec.push(66); diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 75a3b6b..cd11b32 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -7,7 +7,7 @@ // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE + fn main() { let vec0 = Vec::new(); @@ -22,7 +22,7 @@ fn main() { } // `fill_vec()` no longer takes `vec: Vec` as argument -fn fill_vec() -> Vec { +fn fill_vec(vec: Vec) -> Vec { let mut vec = vec; vec.push(22); diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs index 68db09e..1d7e3e5 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -6,13 +6,13 @@ // Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE + fn main() { let mut x = 100; let y = &mut x; - let z = &mut x; *y += 100; + let z = &mut x; *z += 1000; assert_eq!(x, 1200); } diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs index cace4ca..be2906e 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -5,24 +5,24 @@ // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand // for a hint. -// I AM NOT 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); } diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs index 65b7a7f..48229ad 100644 --- a/exercises/vecs/vecs1.rs +++ b/exercises/vecs/vecs1.rs @@ -7,11 +7,11 @@ // // Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + fn array_and_vec() -> ([i32; 4], Vec) { let a = [10, 20, 30, 40]; // a plain array - let v = // TODO: declare your vector here with the macro for vectors + let v = vec![10, 20, 30, 40];// TODO: declare your vector here with the macro for vectors (a, v) } diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs index e92c970..8ca7e71 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -7,13 +7,13 @@ // // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + fn vec_loop(mut v: Vec) -> Vec { for element in v.iter_mut() { // TODO: Fill this up so that each element in the Vec `v` is // multiplied by 2. - ??? + *element *= 2; } // At this point, `v` should be equal to [4, 8, 12, 16, 20]. @@ -24,7 +24,7 @@ fn vec_map(v: &Vec) -> Vec { v.iter().map(|element| { // TODO: Do the same thing as above - but instead of mutating the // Vec, you can just return the new number! - ??? + element*2 }).collect() }