From f66a1bcfa5075728cf9dc8da5448e49f75a262d7 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Tue, 19 Oct 2021 22:08:04 +0200 Subject: [PATCH] add prompt --- Cargo.toml | 2 +- README.md | 6 +++--- src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++------------------ 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef73322..9a9536b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/README.md b/README.md index f1c9d7c..37c10de 100644 --- a/README.md +++ b/README.md @@ -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::().expect("not a number"); + let input = prompt("Guess: ").parse::().expect("not a number"); if input < number { println!("Higher"); } else if input > number { diff --git a/src/lib.rs b/src/lib.rs index 5c4df6e..9a48398 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,29 +1,27 @@ //! //! # 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 { -//!# random_int_range(0..100).to_string() -//!# } -//! let input = input().parse::().expect("not a number"); -//! if input < number { -//! println!("Higher"); -//! } else if input > number { -//! println!("Lower"); -//! } else { -//! println!("Correct!"); -//! break; -//! } +//! let number = random_int_range(0..100); +//! loop { +//!# // hack the input function for this to work in the doc test +//!# fn prompt(_str: &str) -> String { +//!# random_int_range(0..100).to_string() +//!# } +//! let input = prompt("guess: ").parse::().expect("not a number"); +//! if input < number { +//! println!("Higher"); +//! } else if input > number { +//! println!("Lower"); +//! } else { +//! println!("Correct!"); +//! 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 {