1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//! r_tools
//!
//! `r_tools` is a collection of utilities to make the syntax of rust more readable

use std::io::{self, Write};

/// Prints prompt and gets user input
///
/// # Example
/// ```
/// let prompt: String = "What is your name: ";
/// let name: String = r_input(prompt);
/// // User enters name "bob" here
/// assert_eq!(name, "bob".to_string());
/// ```
pub fn r_input(prompt: String) -> String {
    // Taking input
    // Basically the same as an input() in python
    if prompt != "" {
        print!("{}", prompt);
        io::stdout().flush().unwrap();
    }
    let mut result = String::new();
    match std::io::stdin().read_line(&mut result) {
        Ok(_ok) => {
            result = result.replace("\n", "");
        }
        Err(_error) => println!("Error in r_print: {}", _error),
    }
    return result;
}