Merge pull request #1 from angelsflyinhell/master

use Ordering instead of if else
This commit is contained in:
nora 2021-10-19 22:42:36 +02:00 committed by GitHub
commit b71fe50c08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View file

@ -33,19 +33,20 @@ fn main() {
Guessing game Guessing game
```rust ```rust
use std::cmp::Ordering;
use simple_std::{prompt, random_int_range}; use simple_std::{prompt, random_int_range};
fn main() { fn main() {
let number = random_int_range(0..100); let number = random_int_range(0..100);
loop { loop {
let input = prompt("Guess: ").parse::<i32>().expect("not a number"); let input = prompt("Guess: ").parse::<i32>().expect("not a number");
if input < number { match input.cmp(&number) {
println!("Higher"); Ordering::Less => println!("Too Small"),
} else if input > number { Ordering::Greater => println("Too Big"),
println!("Lower"); Ordering::Equal => {
} else { println!("You win!");
println!("Correct!"); break;
break; }
} }
} }
} }

View file

@ -1,6 +1,7 @@
//! //!
//! # Example: guessing game //! # Example: guessing game
//! ``` //! ```
//! use std::cmp::Ordering;
//! use simple_std::{prompt, random_int_range}; //! use simple_std::{prompt, random_int_range};
//! //!
//! let number = random_int_range(0..100); //! let number = random_int_range(0..100);
@ -10,17 +11,18 @@
//!# random_int_range(0..100).to_string() //!# random_int_range(0..100).to_string()
//!# } //!# }
//! let input = prompt("guess: ").parse::<i32>().expect("not a number"); //! let input = prompt("guess: ").parse::<i32>().expect("not a number");
//! if input < number { //! match input.cmp(&number) {
//! println!("Higher"); //! Ordering::Less => println!("Too Small"),
//! } else if input > number { //! Ordering::Greater => println!("Too Big"),
//! println!("Lower"); //! Ordering::Equal => {
//! } else { //! println!("You win!");
//! println!("Correct!"); //! break;
//! break; //! }
//! } //! }
//! } //! }
//! ``` //! ```
pub use io::{input, prompt}; pub use io::{input, prompt};
pub use random::{random_float, random_int_range}; pub use random::{random_float, random_int_range};