Compare commits

..

8 commits

Author SHA1 Message Date
4cc7ae72f9
Fixed documentation spelling error 2021-10-21 16:21:58 +02:00
51554aa8d7 fix missing ! in readme 2021-10-19 22:44:34 +02:00
b71fe50c08
Merge pull request #1 from angelsflyinhell/master
use Ordering instead of if else
2021-10-19 22:42:36 +02:00
46edba9b57
fixed crate doc example 2021-10-19 22:41:45 +02:00
angelsflyinhell
edae442575 idk i thought clion registered my typing last time but turns out it didnt 2021-10-19 22:36:01 +02:00
angelsflyinhell
e09bd41185 exclamation mark 2021-10-19 22:31:57 +02:00
angelsflyinhell
1991397ecf added Ordering use and added example to lib.rs 2021-10-19 22:30:41 +02:00
angelsflyinhell
9a18b81523 use Ordering instead of if else 2021-10-19 22:18:59 +02:00
2 changed files with 21 additions and 18 deletions

View file

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

View file

@ -1,6 +1,7 @@
//!
//! # Example: guessing game
//! ```
//! use std::cmp::Ordering;
//! use simple_std::{prompt, random_int_range};
//!
//! let number = random_int_range(0..100);
@ -10,17 +11,18 @@
//!# random_int_range(0..100).to_string()
//!# }
//! let input = prompt("guess: ").parse::<i32>().expect("not a number");
//! if input < number {
//! println!("Higher");
//! } else if input > number {
//! println!("Lower");
//! } else {
//! println!("Correct!");
//! break;
//! match input.cmp(&number) {
//! Ordering::Less => println!("Too Small"),
//! Ordering::Greater => println!("Too Big"),
//! Ordering::Equal => {
//! println!("You win!");
//! break;
//! }
//! }
//! }
//! ```
pub use io::{input, prompt};
pub use random::{random_float, random_int_range};
@ -90,9 +92,9 @@ mod random {
///
/// # Why is this not in std?
///
/// Rust aims to be correct, that's why it's major random number library is cryptographically secure,
/// meaning it's randomness can't easily be guessed. And cryptographically secure random number generation
/// is a big task, that's why it has it's own crate.
/// Rust aims to be correct, that's why its major random number library is cryptographically secure,
/// meaning its randomness can't easily be guessed. And cryptographically secure random number generation
/// is a big task, that's why it has its own crate.
pub fn random_float() -> f64 {
((random_u64() >> 11) as f64) / ((1u64 << 53) as f64)
}
@ -121,7 +123,7 @@ mod random {
range.start + ((random_u64() as i32).abs() % difference)
}
/// generates a pseudo-random u32
/// generates a pseudo-random u64
fn random_u64() -> u64 {
use std::sync::atomic::{AtomicU64, Ordering};