Complete exercise: idomatic
This commit is contained in:
@@ -8,14 +8,26 @@
|
|||||||
|
|
||||||
// Challenge: Clippy doesn't find *everything*. What else would you change to make this code better?
|
// Challenge: Clippy doesn't find *everything*. What else would you change to make this code better?
|
||||||
|
|
||||||
const pi:f32=3.14159265358979323846;
|
use std::f32::consts::PI;
|
||||||
fn count_to_5()->i32{let mut foo =0;loop{if foo>pi as i32{if foo > 5{break;}}foo=foo+1;}return 5;}
|
|
||||||
|
fn count_to_5() -> i32 {
|
||||||
|
let mut count = 0;
|
||||||
|
loop {
|
||||||
|
if count > PI as i32 && count >= 5 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
count
|
||||||
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("I can count to {}", count_to_5());
|
println!("I can count to {}", count_to_5());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_counting() {
|
fn test_counting() {
|
||||||
assert_eq!(count_to_5() == 5, true);
|
assert_eq!(count_to_5() == 5, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user