Skip to content

Instantly share code, notes, and snippets.

@Coutlaw
Created May 19, 2020 10:39
Show Gist options
  • Save Coutlaw/2501931c07857c46f92da7b9f5b5b266 to your computer and use it in GitHub Desktop.
Save Coutlaw/2501931c07857c46f92da7b9f5b5b266 to your computer and use it in GitHub Desktop.
Rustlings: clippy 1 & 2
// Clippy 1 problem (this does not compile because float comparison)
fn main() {
let x = 1.2331f64;
let y = 1.2332f64;
if y != x {
println!("Success!");
}
}
// Clippy1 solution
fn main() {
let x = 1.2331f64;
let y = 1.2332f64;
if (x - y).abs() < 0.001 {
println!("Success!");
}
}
// Comparing their difference within some error tollerance, in my case 0.001
//Clippy2 problem, does not compile
fn main() {
let mut res = 42;
let option = Some(12);
for x in option {
res += x;
}
println!("{}", res);
}
//Clippy solution (using if let over pattern matching for single-match)
fn main() {
let mut res = 42;
let option = Some(12);
if let Some(x) = option {
res += x;
}
println!("{}", res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment