record progress: clippy, macros
This commit is contained in:
parent
13f58c87bd
commit
7cd24e4973
17 changed files with 153 additions and 44 deletions
|
@ -1,6 +1,6 @@
|
||||||
DON'T EDIT THIS FILE!
|
DON'T EDIT THIS FILE!
|
||||||
|
|
||||||
macros1
|
using_as
|
||||||
|
|
||||||
intro1
|
intro1
|
||||||
intro2
|
intro2
|
||||||
|
@ -83,4 +83,11 @@ arc1
|
||||||
cow1
|
cow1
|
||||||
threads1
|
threads1
|
||||||
threads2
|
threads2
|
||||||
threads3
|
threads3
|
||||||
|
macros1
|
||||||
|
macros2
|
||||||
|
macros3
|
||||||
|
macros4
|
||||||
|
clippy1
|
||||||
|
clippy2
|
||||||
|
clippy3
|
|
@ -6,5 +6,5 @@ macro_rules! my_macro {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// TODO: Fix the macro call.
|
// TODO: Fix the macro call.
|
||||||
my_macro();
|
my_macro!();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
fn main() {
|
|
||||||
my_macro!();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Fix the compiler error by moving the whole definition of this macro.
|
|
||||||
macro_rules! my_macro {
|
macro_rules! my_macro {
|
||||||
() => {
|
() => {
|
||||||
println!("Check out my macro!");
|
println!("Check out my macro!");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
my_macro!();
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// TODO: Fix the compiler error without taking the macro definition out of this
|
// TODO: Fix the compiler error without taking the macro definition out of this
|
||||||
// module.
|
// module.
|
||||||
|
#[macro_use]
|
||||||
mod macros {
|
mod macros {
|
||||||
macro_rules! my_macro {
|
macro_rules! my_macro {
|
||||||
() => {
|
() => {
|
||||||
|
|
|
@ -3,10 +3,10 @@
|
||||||
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() {
|
||||||
|
|
|
@ -4,9 +4,11 @@
|
||||||
// For these exercises, the code will fail to compile when there are Clippy
|
// For these exercises, the code will fail to compile when there are Clippy
|
||||||
// warnings. Check Clippy's suggestions from the output to solve the exercise.
|
// warnings. Check Clippy's suggestions from the output to solve the exercise.
|
||||||
|
|
||||||
|
use std::f32::consts;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// TODO: Fix the Clippy lint in this line.
|
// TODO: Fix the Clippy lint in this line.
|
||||||
let pi = 3.14;
|
let pi = consts::PI;
|
||||||
let radius: f32 = 5.0;
|
let radius: f32 = 5.0;
|
||||||
|
|
||||||
let area = pi * radius.powi(2);
|
let area = pi * radius.powi(2);
|
||||||
|
|
|
@ -2,7 +2,7 @@ fn main() {
|
||||||
let mut res = 42;
|
let mut res = 42;
|
||||||
let option = Some(12);
|
let option = Some(12);
|
||||||
// TODO: Fix the Clippy lint.
|
// TODO: Fix the Clippy lint.
|
||||||
for x in option {
|
if let Some(x) = option {
|
||||||
res += x;
|
res += x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,22 +6,22 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_option: Option<()> = None;
|
let my_option: Option<()> = None;
|
||||||
if my_option.is_none() {
|
if my_option.is_none() {
|
||||||
println!("{:?}", my_option.unwrap());
|
println!("{:?}", my_option);
|
||||||
}
|
}
|
||||||
|
|
||||||
let my_arr = &[
|
let my_arr = &[
|
||||||
-1, -2, -3
|
-1, -2, -3,
|
||||||
-4, -5, -6
|
-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.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;
|
std::mem::swap(&mut value_a, &mut value_b);
|
||||||
value_b = value_a;
|
|
||||||
println!("value a: {value_a}; value b: {value_b}");
|
println!("value a: {value_a}; value b: {value_b}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,21 +3,19 @@
|
||||||
// https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
// https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
||||||
|
|
||||||
// Obtain the number of bytes (not characters) in the given argument.
|
// Obtain the number of bytes (not characters) in the given argument.
|
||||||
// TODO: Add the `AsRef` trait appropriately as a trait bound.
|
fn byte_counter<T>(arg: T) -> usize where T: AsRef<str>
|
||||||
fn byte_counter<T>(arg: T) -> usize {
|
{
|
||||||
arg.as_ref().as_bytes().len()
|
arg.as_ref().as_bytes().len()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtain the number of characters (not bytes) in the given argument.
|
// Obtain the number of characters (not bytes) in the given argument.
|
||||||
// TODO: Add the `AsRef` trait appropriately as a trait bound.
|
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
|
||||||
fn char_counter<T>(arg: T) -> usize {
|
|
||||||
arg.as_ref().chars().count()
|
arg.as_ref().chars().count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Squares a number using `as_mut()`.
|
// Squares a number using `as_mut()`.
|
||||||
// TODO: Add the appropriate trait bound.
|
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
|
||||||
fn num_sq<T>(arg: &mut T) {
|
*arg.as_mut() = arg.as_mut().pow(2);
|
||||||
// TODO: Implement the function body.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -20,7 +20,7 @@ impl Default for Person {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Complete this `From` implementation to be able to parse a `Person`
|
// Complete this `From` implementation to be able to parse a `Person`
|
||||||
// out of a string in the form of "Mark,20".
|
// out of a string in the form of "Mark,20".
|
||||||
// Note that you'll need to parse the age component into a `u8` with something
|
// Note that you'll need to parse the age component into a `u8` with something
|
||||||
// like `"4".parse::<u8>()`.
|
// like `"4".parse::<u8>()`.
|
||||||
|
@ -34,7 +34,31 @@ impl Default for Person {
|
||||||
// 5. Parse the second element from the split operation into a `u8` as the age.
|
// 5. Parse the second element from the split operation into a `u8` as the age.
|
||||||
// 6. If parsing the age fails, return the default of `Person`.
|
// 6. If parsing the age fails, return the default of `Person`.
|
||||||
impl From<&str> for Person {
|
impl From<&str> for Person {
|
||||||
fn from(s: &str) -> Self {}
|
fn from(s: &str) -> Self {
|
||||||
|
let elements: Vec<&str> = s.split(",").collect();
|
||||||
|
|
||||||
|
match elements.as_slice() {
|
||||||
|
[a, b] => {
|
||||||
|
if let Ok(age) = b.parse::<u8>() {
|
||||||
|
let name = a.parse().unwrap();
|
||||||
|
if name == "" {
|
||||||
|
return Person {
|
||||||
|
name: String::from("John"),
|
||||||
|
age: age,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return Person {
|
||||||
|
name: name,
|
||||||
|
age: age,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
Person::default()
|
||||||
|
}
|
||||||
|
&_ => {
|
||||||
|
Person::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
fn main() {
|
macro_rules! my_macro {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
() => {
|
||||||
// It will be automatically filled after you finish the exercise.
|
println!("Check out my macro!");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
my_macro!();
|
||||||
|
// ^
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
fn main() {
|
// Moved the macro definition to be before its call.
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
macro_rules! my_macro {
|
||||||
// It will be automatically filled after you finish the exercise.
|
() => {
|
||||||
|
println!("Check out my macro!");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
my_macro!();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,13 @@
|
||||||
fn main() {
|
// Added the attribute `macro_use` attribute.
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
#[macro_use]
|
||||||
// It will be automatically filled after you finish the exercise.
|
mod macros {
|
||||||
|
macro_rules! my_macro {
|
||||||
|
() => {
|
||||||
|
println!("Check out my macro!");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
my_macro!();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,15 @@
|
||||||
fn main() {
|
// Added semicolons to separate the macro arms.
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
#[rustfmt::skip]
|
||||||
// It will be automatically filled after you finish the exercise.
|
macro_rules! my_macro {
|
||||||
|
() => {
|
||||||
|
println!("Check out my macro!");
|
||||||
|
};
|
||||||
|
($val:expr) => {
|
||||||
|
println!("Look at this other macro: {}", $val);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
my_macro!();
|
||||||
|
my_macro!(7777);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,17 @@
|
||||||
|
// The Clippy tool is a collection of lints to analyze your code so you can
|
||||||
|
// catch common mistakes and improve your Rust code.
|
||||||
|
//
|
||||||
|
// For these exercises, the code will fail to compile when there are Clippy
|
||||||
|
// warnings. Check Clippy's suggestions from the output to solve the exercise.
|
||||||
|
|
||||||
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
// Use the more accurate `PI` constant.
|
||||||
// It will be automatically filled after you finish the exercise.
|
let pi = PI;
|
||||||
|
let radius: f32 = 5.0;
|
||||||
|
|
||||||
|
let area = pi * radius.powi(2);
|
||||||
|
|
||||||
|
println!("The area of a circle with radius {radius:.2} is {area:.5}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
let mut res = 42;
|
||||||
// It will be automatically filled after you finish the exercise.
|
let option = Some(12);
|
||||||
|
// Use `if-let` instead of iteration.
|
||||||
|
if let Some(x) = option {
|
||||||
|
res += x;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{res}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,31 @@
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
#[allow(unused_variables, unused_assignments)]
|
||||||
fn main() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
let my_option: Option<()> = None;
|
||||||
// It will be automatically filled after you finish the exercise.
|
// `unwrap` of an `Option` after checking if it is `None` will panic.
|
||||||
|
// Use `if-let` instead.
|
||||||
|
if let Some(value) = my_option {
|
||||||
|
println!("{value:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// A comma was missing.
|
||||||
|
let my_arr = &[
|
||||||
|
-1, -2, -3,
|
||||||
|
-4, -5, -6,
|
||||||
|
];
|
||||||
|
println!("My array! Here it is: {:?}", my_arr);
|
||||||
|
|
||||||
|
let mut my_empty_vec = vec![1, 2, 3, 4, 5];
|
||||||
|
// `resize` mutates a vector instead of returning a new one.
|
||||||
|
// `resize(0, …)` clears a vector, so it is better to use `clear`.
|
||||||
|
my_empty_vec.clear();
|
||||||
|
println!("This Vec is empty, see? {my_empty_vec:?}");
|
||||||
|
|
||||||
|
let mut value_a = 45;
|
||||||
|
let mut value_b = 66;
|
||||||
|
// Use `mem::swap` to correctly swap two values.
|
||||||
|
mem::swap(&mut value_a, &mut value_b);
|
||||||
|
println!("value a: {}; value b: {}", value_a, value_b);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue