simple_input/lib.rs
1//! # Simple Input
2//!
3//! `simple_input` provides a simple way to ask for user input, inspired by Python's `input` function.
4//!
5//! ```
6//! extern crate simple_input;
7//! use simple_input::input;
8//!
9//! fn main() {
10//! let name = input("What is your name? ");
11//! println!("Hello {}!", name);
12//! }
13//! ```
14
15use std::io;
16
17/// Takes user input, given a prompt.
18///
19/// # Example
20///
21/// ```
22/// let user_input = input("What is your name? ");
23/// println!("Hello {}!", user_input);
24/// ```
25pub fn input(prompt: &str) -> String {
26 let mut user_input = String::new();
27 print!("{}", prompt); // Print the prompt
28
29 io::Write::flush(&mut io::stdout()) // Flush stdout
30 .expect("Stdout flush failed while taking user input");
31
32 io::stdin().read_line(&mut user_input) // Accept user input
33 .expect("Taking user input from Stdin failed");
34
35 user_input.pop(); // Remove the newline character
36 return user_input;
37}