From 460340955eb1141f9586ced245648fa2b1bdcf61 Mon Sep 17 00:00:00 2001 From: matytan <-f> Date: Thu, 17 Nov 2022 19:37:30 +0800 Subject: [PATCH] day2 --- exercises/move_semantics/move_semantics1.rs | 5 ++--- exercises/move_semantics/move_semantics2.rs | 13 +++++++------ exercises/move_semantics/move_semantics3.rs | 7 +++++-- exercises/move_semantics/move_semantics4.rs | 8 +++----- exercises/move_semantics/move_semantics5.rs | 5 ++--- exercises/move_semantics/move_semantics6.rs | 14 +++++++------- exercises/primitive_types/primitive_types4.rs | 4 +--- exercises/primitive_types/primitive_types5.rs | 3 +-- exercises/primitive_types/primitive_types6.rs | 7 ++----- exercises/structs/structs1.rs | 17 ++++++++++++----- exercises/vecs/vecs1.rs | 4 +--- exercises/vecs/vecs2.rs | 16 ++++++++-------- 12 files changed, 51 insertions(+), 52 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index aac6dfc3..ec3e0209 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,12 +1,11 @@ // move_semantics1.rs // 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 64870850..0aa2d84d 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -2,27 +2,28 @@ // Make me compile without changing line 13 or moving line 10! // 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 vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(&mut vec0); // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); vec1.push(88); + println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { +//错误1:只传引用,绑定的新 vec为引用,导致值并不可变,所以 push 会出错 +fn fill_vec(vec: &mut Vec) -> Vec { let mut vec = vec; vec.push(22); vec.push(44); vec.push(66); + vec.iter().map(|x| x * 1).collect() - vec + // *vec1.iter().map(|x| x * 1).collect::>().to_vec() } diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index eaa30e33..f10bdf25 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -3,8 +3,6 @@ // (no lines with multiple semicolons necessary!) // Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); @@ -18,6 +16,11 @@ fn main() { } fn fill_vec(vec: Vec) -> Vec { + //不可变可以绑定给可变 + //可变也可以绑定不可变 + //不可变引用借用给mutable变量,但是值仍然不可变 + //mutable 引用可以借用给不可变,同样值不变 + 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 99834ec3..559f69a8 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -4,12 +4,10 @@ // function. // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { - let vec0 = Vec::new(); + // 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 +18,7 @@ fn main() { // `fill_vec()` no longer takes `vec: Vec` as argument fn fill_vec() -> Vec { - let mut vec = vec; + let mut vec = Vec::new(); vec.push(22); vec.push(44); diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs index 36eae127..67ed8fc7 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -3,13 +3,12 @@ // adding, changing or removing any of them. // 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 eb52a848..59d7c1ce 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -2,24 +2,24 @@ // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand for a hint. // You can't change anything except adding or removing references. -// I AM NOT DONE - fn main() { let data = "Rust is great!".to_string(); - get_char(data); + // get_char(&data); + println!("{:?}", 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(data: &mut String) { +fn string_uppercase(mut data: String) { + data = data.to_uppercase(); println!("{}", data); } diff --git a/exercises/primitive_types/primitive_types4.rs b/exercises/primitive_types/primitive_types4.rs index 2d8d59c9..e3371784 100644 --- a/exercises/primitive_types/primitive_types4.rs +++ b/exercises/primitive_types/primitive_types4.rs @@ -2,13 +2,11 @@ // Get a slice out of Array a where the ??? is so that the test passes. // Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; - let nice_slice = &a[1..=5]; + let nice_slice = &a[1..4]; assert_eq!([2, 3, 4], nice_slice) } diff --git a/exercises/primitive_types/primitive_types5.rs b/exercises/primitive_types/primitive_types5.rs index 4fd9141f..76a92b88 100644 --- a/exercises/primitive_types/primitive_types5.rs +++ b/exercises/primitive_types/primitive_types5.rs @@ -2,11 +2,10 @@ // Destructure the `cat` tuple so that the println will work. // Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE fn main() { let cat = ("Furry McFurson", 3.5); - let /* your pattern here */ = cat; + let (name, age) = cat; println!("{} is {} years old.", name, age); } diff --git a/exercises/primitive_types/primitive_types6.rs b/exercises/primitive_types/primitive_types6.rs index ddf8b423..4d614e57 100644 --- a/exercises/primitive_types/primitive_types6.rs +++ b/exercises/primitive_types/primitive_types6.rs @@ -3,14 +3,11 @@ // You can put the expression for the second element where ??? is so that the test passes. // Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[test] fn indexing_tuple() { let numbers = (1, 2, 3); // Replace below ??? with the tuple indexing syntax. - let second = ???; + let second = numbers.1; - assert_eq!(2, second, - "This is not the 2nd number in the tuple!") + assert_eq!(2, second, "This is not the 2nd number in the tuple!") } diff --git a/exercises/structs/structs1.rs b/exercises/structs/structs1.rs index 0d91c469..17496897 100644 --- a/exercises/structs/structs1.rs +++ b/exercises/structs/structs1.rs @@ -6,9 +6,12 @@ struct ColorClassicStruct { // TODO: Something goes here + red: i32, + green: i32, + blue: i32, } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct(/* TODO: Something goes here */ i32, i32, i32); #[derive(Debug)] struct UnitLikeStruct; @@ -20,7 +23,12 @@ mod tests { #[test] fn classic_c_structs() { // TODO: Instantiate a classic c struct! - // let green = + // let green ={} + let green = ColorClassicStruct { + red: 0, + green: 255, + blue: 0, + }; assert_eq!(green.red, 0); assert_eq!(green.green, 255); @@ -30,8 +38,7 @@ mod tests { #[test] fn tuple_structs() { // TODO: Instantiate a tuple struct! - // let green = - + let green = ColorTupleStruct(0, 255, 0); assert_eq!(green.0, 0); assert_eq!(green.1, 255); assert_eq!(green.2, 0); @@ -40,7 +47,7 @@ mod tests { #[test] fn unit_structs() { // TODO: Instantiate a unit-like struct! - // let unit_like_struct = + let unit_like_struct = UnitLikeStruct {}; let message = format!("{:?}s are fun!", unit_like_struct); assert_eq!(message, "UnitLikeStructs are fun!"); diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs index 4e8c4cbb..e0c9ae45 100644 --- a/exercises/vecs/vecs1.rs +++ b/exercises/vecs/vecs1.rs @@ -4,11 +4,9 @@ // Make me compile and pass the test! // 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 = 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 5bea09a2..8ac17c00 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -6,13 +6,11 @@ // // 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 i in v.iter_mut() { // TODO: Fill this up so that each element in the Vec `v` is // multiplied by 2. - ??? + *i = *i * 2; } // At this point, `v` should be equal to [4, 8, 12, 16, 20]. @@ -20,11 +18,13 @@ fn vec_loop(mut v: Vec) -> Vec { } fn vec_map(v: &Vec) -> Vec { - v.iter().map(|num| { - // TODO: Do the same thing as above - but instead of mutating the - // Vec, you can just return the new number! - ??? - }).collect() + v.iter() + .map(|num| { + // TODO: Do the same thing as above - but instead of mutating the + // Vec, you can just return the new number! + num * 2 + }) + .collect() } #[cfg(test)]