essentials/
io.rs

1use std::io::{self, Error as IoError, Read, Result as IoResult, Write};
2
3pub trait ReadExt: Read {
4    fn read_to_new_string(&mut self) -> IoResult<String> {
5        let mut s = String::new();
6        self.read_to_string(&mut s)?;
7
8        Ok(s)
9    }
10}
11
12pub fn prompt(message: &str) -> Result<String, IoError> {
13    {
14        let out = io::stdout();
15        let mut lock = out.lock();
16        write!(lock, "{}", message)?;
17        lock.flush()?;
18    }
19
20    let mut s = String::new();
21    io::stdin().read_line(&mut s)?;
22
23    Ok(s)
24}
25
26#[cfg(test)]
27mod tests {
28    use super::ReadExt;
29
30    static_assertions::assert_obj_safe!(ReadExt);
31}