mirror of
https://github.com/Noratrieb/simple-std.git
synced 2026-01-14 16:35:06 +01:00
add prompt
This commit is contained in:
parent
e821f7fb12
commit
f66a1bcfa5
3 changed files with 42 additions and 22 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "simple-std"
|
name = "simple-std"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "A simple extension to the Rust standard library for exercises"
|
description = "A simple extension to the Rust standard library for exercises"
|
||||||
keywords = ["beginner", "help"]
|
keywords = ["beginner", "help"]
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ really, don't.
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
simple-std = "0.1.0"
|
simple-std = "0.1.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
simple-std is a little extension to the standard library,
|
simple-std is a little extension to the standard library,
|
||||||
|
|
@ -33,12 +33,12 @@ fn main() {
|
||||||
Guessing game
|
Guessing game
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use simple_std::{input, 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 = input().parse::<i32>().expect("not a number");
|
let input = prompt("Guess: ").parse::<i32>().expect("not a number");
|
||||||
if input < number {
|
if input < number {
|
||||||
println!("Higher");
|
println!("Higher");
|
||||||
} else if input > number {
|
} else if input > number {
|
||||||
|
|
|
||||||
56
src/lib.rs
56
src/lib.rs
|
|
@ -1,29 +1,27 @@
|
||||||
//!
|
//!
|
||||||
//! # Example: guessing game
|
//! # Example: guessing game
|
||||||
//! ```
|
//! ```
|
||||||
//! use simple_std::{input, random_int_range};
|
//! use simple_std::{prompt, random_int_range};
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! let number = random_int_range(0..100);
|
||||||
//! let number = random_int_range(0..100);
|
//! loop {
|
||||||
//! loop {
|
//!# // hack the input function for this to work in the doc test
|
||||||
//!# // hack the input function for this to work in the doc test
|
//!# fn prompt(_str: &str) -> String {
|
||||||
//!# fn input() -> String {
|
//!# 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 = input().parse::<i32>().expect("not a number");
|
//! if input < number {
|
||||||
//! if input < number {
|
//! println!("Higher");
|
||||||
//! println!("Higher");
|
//! } else if input > number {
|
||||||
//! } else if input > number {
|
//! println!("Lower");
|
||||||
//! println!("Lower");
|
//! } else {
|
||||||
//! } else {
|
//! println!("Correct!");
|
||||||
//! println!("Correct!");
|
//! break;
|
||||||
//! break;
|
|
||||||
//! }
|
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
pub use io::input;
|
pub use io::{input, prompt};
|
||||||
pub use random::{random_float, random_int_range};
|
pub use random::{random_float, random_int_range};
|
||||||
|
|
||||||
mod io {
|
mod io {
|
||||||
|
|
@ -48,6 +46,28 @@ mod io {
|
||||||
std::io::stdin().read_line(&mut buffer).unwrap();
|
std::io::stdin().read_line(&mut buffer).unwrap();
|
||||||
buffer
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Reads a single line of input, while providing a message that comes on the same line.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use simple_std::prompt;
|
||||||
|
///
|
||||||
|
/// let name = prompt("Your name: ");
|
||||||
|
/// println!("Hello {}!", name)
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Why is this not in std?
|
||||||
|
///
|
||||||
|
/// see [`input`]
|
||||||
|
pub fn prompt(message: &str) -> String {
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
print!("{}", message);
|
||||||
|
std::io::stdout().flush().unwrap();
|
||||||
|
input()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod random {
|
mod random {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue