mirror of
https://github.com/Noratrieb/simple-std.git
synced 2026-01-16 17:35:06 +01:00
Compare commits
8 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4cc7ae72f9 | |||
| 51554aa8d7 | |||
| b71fe50c08 | |||
| 46edba9b57 | |||
|
|
edae442575 | ||
|
|
e09bd41185 | ||
|
|
1991397ecf | ||
|
|
9a18b81523 |
2 changed files with 21 additions and 18 deletions
13
README.md
13
README.md
|
|
@ -33,20 +33,21 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
22
src/lib.rs
22
src/lib.rs
|
|
@ -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};
|
||||||
|
|
||||||
|
|
@ -90,9 +92,9 @@ mod random {
|
||||||
///
|
///
|
||||||
/// # Why is this not in std?
|
/// # Why is this not in std?
|
||||||
///
|
///
|
||||||
/// Rust aims to be correct, that's why it's major random number library is cryptographically secure,
|
/// Rust aims to be correct, that's why its major random number library is cryptographically secure,
|
||||||
/// meaning it's randomness can't easily be guessed. And cryptographically secure random number generation
|
/// meaning its 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.
|
/// is a big task, that's why it has its own crate.
|
||||||
pub fn random_float() -> f64 {
|
pub fn random_float() -> f64 {
|
||||||
((random_u64() >> 11) as f64) / ((1u64 << 53) as f64)
|
((random_u64() >> 11) as f64) / ((1u64 << 53) as f64)
|
||||||
}
|
}
|
||||||
|
|
@ -121,7 +123,7 @@ mod random {
|
||||||
range.start + ((random_u64() as i32).abs() % difference)
|
range.start + ((random_u64() as i32).abs() % difference)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// generates a pseudo-random u32
|
/// generates a pseudo-random u64
|
||||||
fn random_u64() -> u64 {
|
fn random_u64() -> u64 {
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue