mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
day12
This commit is contained in:
parent
cfc401e1c7
commit
c2b9b200d2
@ -6,12 +6,12 @@
|
|||||||
// check clippy's suggestions from the output to solve the exercise.
|
// check clippy's suggestions from the output to solve the exercise.
|
||||||
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// cargo clippy
|
||||||
|
|
||||||
use std::f32;
|
use std::f32;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let pi = 3.14f32;
|
let pi = f32::consts::PI;
|
||||||
let radius = 5.00f32;
|
let radius = 5.00f32;
|
||||||
|
|
||||||
let area = pi * f32::powi(radius, 2);
|
let area = pi * f32::powi(radius, 2);
|
||||||
|
|||||||
@ -1,12 +1,10 @@
|
|||||||
// clippy2.rs
|
// clippy2.rs
|
||||||
// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut res = 42;
|
let mut res = 42;
|
||||||
let option = Some(12);
|
let option = Some(12);
|
||||||
for x in option {
|
if let Some(x) = option {
|
||||||
res += x;
|
res += x;
|
||||||
}
|
}
|
||||||
println!("{}", res);
|
println!("{}", res);
|
||||||
|
|||||||
@ -1,28 +1,30 @@
|
|||||||
// clippy3.rs
|
// clippy3.rs
|
||||||
// Here's a couple more easy Clippy fixes, so you can see its utility.
|
// Here's a couple more easy Clippy fixes, so you can see its utility.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[allow(unused_variables, unused_assignments)]
|
#[allow(unused_variables, unused_assignments)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_option: Option<()> = None;
|
let my_option: Option<()> = None;
|
||||||
if my_option.is_none() {
|
if my_option.is_none() {
|
||||||
my_option.unwrap();
|
// my_option.unwrap();
|
||||||
|
println!("my_option is none");
|
||||||
}
|
}
|
||||||
|
|
||||||
let my_arr = &[
|
let my_arr = &[-1, -2, -3 - 4, -5, -6];
|
||||||
-1, -2, -3
|
|
||||||
-4, -5, -6
|
|
||||||
];
|
|
||||||
println!("My array! Here it is: {:?}", my_arr);
|
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);
|
println!("This Vec is empty, see? {:?}", my_empty_vec);
|
||||||
|
|
||||||
let mut value_a = 45;
|
let mut value_a = 45;
|
||||||
let mut value_b = 66;
|
let mut value_b = 66;
|
||||||
// Let's swap these two!
|
// Let's swap these two!
|
||||||
value_a = value_b;
|
// value_a = value_b;
|
||||||
value_b = value_a;
|
// 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);
|
println!("value a: {}; value b: {}", value_a, value_b);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
// If From is implemented correctly for a type, the Into trait should work conversely.
|
// 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
|
// 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.
|
// Execute `rustlings hint from_into` or use the `hint` watch subcommand for a hint.
|
||||||
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Person {
|
struct Person {
|
||||||
@ -27,18 +28,50 @@ impl Default for Person {
|
|||||||
// be handled appropriately.
|
// be handled appropriately.
|
||||||
//
|
//
|
||||||
// Steps:
|
// Steps:
|
||||||
// 1. If the length of the provided string is 0, then return the default of Person
|
// 1. If the length of the provided string is 0, then return the default of Person 如果为0
|
||||||
// 2. Split the given string on the commas present in it
|
// 返回defalut
|
||||||
// 3. Extract the first element from the split operation and use it as the name
|
// 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
|
// 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
|
// 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
|
// If while parsing the age, something goes wrong, then return the default of Person
|
||||||
// Otherwise, then return an instantiated Person object with the results
|
// Otherwise, then return an instantiated Person object with the results
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
impl From<&str> for Person {
|
impl From<&str> for Person {
|
||||||
fn from(s: &str) -> 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,11 +6,9 @@
|
|||||||
// and returns the proper type.
|
// and returns the proper type.
|
||||||
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn average(values: &[f64]) -> f64 {
|
fn average(values: &[f64]) -> f64 {
|
||||||
let total = values.iter().sum::<f64>();
|
let total = values.iter().sum::<f64>();
|
||||||
total / values.len()
|
total / values.len() as f64
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -2,8 +2,6 @@
|
|||||||
// Make me compile, without taking the macro out of the module!
|
// Make me compile, without taking the macro out of the module!
|
||||||
// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros {
|
mod macros {
|
||||||
macro_rules! my_macro {
|
macro_rules! my_macro {
|
||||||
|
|||||||
@ -1,15 +1,14 @@
|
|||||||
// macros4.rs
|
// macros4.rs
|
||||||
// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
#[macro_export]
|
||||||
|
|
||||||
macro_rules! my_macro {
|
macro_rules! my_macro {
|
||||||
() => {
|
() => {
|
||||||
println!("Check out my macro!");
|
println!("Check out my macro!");
|
||||||
}
|
};
|
||||||
($val:expr) => {
|
($val:expr) => {
|
||||||
println!("Look at this other macro: {}", $val);
|
println!("Look at this other macro: {}", $val);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user