This commit is contained in:
MatyTan 2022-11-27 10:38:11 +08:00
parent cfc401e1c7
commit c2b9b200d2
7 changed files with 57 additions and 29 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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<usize, ParseIntError> = age.parse();
match res {
Ok(a) => p.age = a,
Err(err) => {
println!("{:?}", err);
return default;
}
};
p
}
}

View File

@ -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::<f64>();
total / values.len()
total / values.len() as f64
}
fn main() {

View File

@ -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 {

View File

@ -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() {