From c2b9b200d23d733bcbe63ea3193ca4be59f8e064 Mon Sep 17 00:00:00 2001 From: MatyTan <1396568121@qq.com> Date: Sun, 27 Nov 2022 10:38:11 +0800 Subject: [PATCH] day12 --- exercises/clippy/clippy1.rs | 4 +-- exercises/clippy/clippy2.rs | 4 +-- exercises/clippy/clippy3.rs | 22 ++++++++------- exercises/conversions/from_into.rs | 43 ++++++++++++++++++++++++++---- exercises/conversions/using_as.rs | 4 +-- exercises/macros/macros3.rs | 2 -- exercises/macros/macros4.rs | 7 +++-- 7 files changed, 57 insertions(+), 29 deletions(-) diff --git a/exercises/clippy/clippy1.rs b/exercises/clippy/clippy1.rs index bad46891..189b3af3 100644 --- a/exercises/clippy/clippy1.rs +++ b/exercises/clippy/clippy1.rs @@ -6,12 +6,12 @@ // check clippy's suggestions from the output to solve the exercise. // Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE +// cargo clippy use std::f32; fn main() { - let pi = 3.14f32; + let pi = f32::consts::PI; let radius = 5.00f32; let area = pi * f32::powi(radius, 2); diff --git a/exercises/clippy/clippy2.rs b/exercises/clippy/clippy2.rs index dac40dbe..48d82440 100644 --- a/exercises/clippy/clippy2.rs +++ b/exercises/clippy/clippy2.rs @@ -1,12 +1,10 @@ // clippy2.rs // Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let mut res = 42; let option = Some(12); - for x in option { + if let Some(x) = option { res += x; } println!("{}", res); diff --git a/exercises/clippy/clippy3.rs b/exercises/clippy/clippy3.rs index b0159ebe..9fa4b727 100644 --- a/exercises/clippy/clippy3.rs +++ b/exercises/clippy/clippy3.rs @@ -1,28 +1,30 @@ // clippy3.rs // Here's a couple more easy Clippy fixes, so you can see its utility. -// I AM NOT DONE - #[allow(unused_variables, unused_assignments)] fn main() { let my_option: Option<()> = None; if my_option.is_none() { - my_option.unwrap(); + // my_option.unwrap(); + println!("my_option is none"); } - let my_arr = &[ - -1, -2, -3 - -4, -5, -6 - ]; + let my_arr = &[-1, -2, -3 - 4, -5, -6]; println!("My array! Here it is: {:?}", my_arr); - let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5); + let mut my_empty_vec = vec![1, 2, 3, 4, 5]; + // my_empty_vec.resize(0, 5); + my_empty_vec.clear(); println!("This Vec is empty, see? {:?}", my_empty_vec); let mut value_a = 45; let mut value_b = 66; // Let's swap these two! - value_a = value_b; - value_b = value_a; + // value_a = value_b; + // value_b = value_a; + // let temp = value_a; + // value_a = value_b; + // value_b = temp; + std::mem::swap(&mut value_a, &mut value_b); println!("value a: {}; value b: {}", value_a, value_b); } diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index 6c272c3b..850811e0 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -2,6 +2,7 @@ // If From is implemented correctly for a type, the Into trait should work conversely. // You can read more about it at https://doc.rust-lang.org/std/convert/trait.From.html // Execute `rustlings hint from_into` or use the `hint` watch subcommand for a hint. +use std::num::ParseIntError; #[derive(Debug)] struct Person { @@ -27,18 +28,50 @@ impl Default for Person { // be handled appropriately. // // Steps: -// 1. If the length of the provided string is 0, then return the default of Person -// 2. Split the given string on the commas present in it -// 3. Extract the first element from the split operation and use it as the name +// 1. If the length of the provided string is 0, then return the default of Person 如果为0 +// 返回defalut +// 2. Split the given string on the commas present in it 逗号分割 +// 3. Extract the first element from the split operation and use it as the name 第一个元素 // 4. If the name is empty, then return the default of Person // 5. Extract the other element from the split operation and parse it into a `usize` as the age +// 提取年龄 // If while parsing the age, something goes wrong, then return the default of Person // Otherwise, then return an instantiated Person object with the results -// I AM NOT DONE - impl From<&str> for Person { fn from(s: &str) -> Person { + let default = Person::default(); + let mut p = Person { + name: String::from(""), + age: 0, + }; + if s.len() == 0 { + return default; + } + + let person: Vec<&str> = s.split(",").collect(); + if person.len() != 2 { + return default; + } + + let name = person[0]; + + if name.len() <= 0 { + return default; + } + p.name = name.to_string(); + + let age = person[1]; + let res: Result = age.parse(); + match res { + Ok(a) => p.age = a, + Err(err) => { + println!("{:?}", err); + return default; + } + }; + + p } } diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs index 8c9b7113..0d961ca0 100644 --- a/exercises/conversions/using_as.rs +++ b/exercises/conversions/using_as.rs @@ -6,11 +6,9 @@ // and returns the proper type. // Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn average(values: &[f64]) -> f64 { let total = values.iter().sum::(); - total / values.len() + total / values.len() as f64 } fn main() { diff --git a/exercises/macros/macros3.rs b/exercises/macros/macros3.rs index a558c02e..302da074 100644 --- a/exercises/macros/macros3.rs +++ b/exercises/macros/macros3.rs @@ -2,8 +2,6 @@ // Make me compile, without taking the macro out of the module! // Execute `rustlings hint macros3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[macro_use] mod macros { macro_rules! my_macro { diff --git a/exercises/macros/macros4.rs b/exercises/macros/macros4.rs index c1fc5e8b..f7cc8c0c 100644 --- a/exercises/macros/macros4.rs +++ b/exercises/macros/macros4.rs @@ -1,15 +1,14 @@ // macros4.rs // Execute `rustlings hint macros4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - +#[macro_export] macro_rules! my_macro { () => { println!("Check out my macro!"); - } + }; ($val:expr) => { println!("Look at this other macro: {}", $val); - } + }; } fn main() {