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]
|
||||
name = "simple-std"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2018"
|
||||
description = "A simple extension to the Rust standard library for exercises"
|
||||
keywords = ["beginner", "help"]
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ really, don't.
|
|||
|
||||
```toml
|
||||
[dependencies]
|
||||
simple-std = "0.1.0"
|
||||
simple-std = "0.1.1"
|
||||
```
|
||||
|
||||
simple-std is a little extension to the standard library,
|
||||
|
|
@ -33,12 +33,12 @@ fn main() {
|
|||
Guessing game
|
||||
|
||||
```rust
|
||||
use simple_std::{input, random_int_range};
|
||||
use simple_std::{prompt, random_int_range};
|
||||
|
||||
fn main() {
|
||||
let number = random_int_range(0..100);
|
||||
loop {
|
||||
let input = input().parse::<i32>().expect("not a number");
|
||||
let input = prompt("Guess: ").parse::<i32>().expect("not a number");
|
||||
if input < number {
|
||||
println!("Higher");
|
||||
} else if input > number {
|
||||
|
|
|
|||
32
src/lib.rs
32
src/lib.rs
|
|
@ -1,16 +1,15 @@
|
|||
//!
|
||||
//! # 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);
|
||||
//! loop {
|
||||
//!# // hack the input function for this to work in the doc test
|
||||
//!# fn input() -> String {
|
||||
//!# fn prompt(_str: &str) -> String {
|
||||
//!# random_int_range(0..100).to_string()
|
||||
//!# }
|
||||
//! let input = input().parse::<i32>().expect("not a number");
|
||||
//! let input = prompt("guess: ").parse::<i32>().expect("not a number");
|
||||
//! if input < number {
|
||||
//! println!("Higher");
|
||||
//! } else if input > number {
|
||||
|
|
@ -20,10 +19,9 @@
|
|||
//! break;
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
pub use io::input;
|
||||
pub use io::{input, prompt};
|
||||
pub use random::{random_float, random_int_range};
|
||||
|
||||
mod io {
|
||||
|
|
@ -48,6 +46,28 @@ mod io {
|
|||
std::io::stdin().read_line(&mut buffer).unwrap();
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue