input_rust/
lib.rs

1use std::fmt::Display;
2use std::io::{self, Write};
3
4pub fn input(msg: impl Display) -> io::Result<String> {
5    print!("{}", msg.to_string());
6    io::stdout().flush()?;
7
8    let mut text = String::new();
9    match io::stdin().read_line(&mut text) {
10        Ok(_) => {
11            let trimmed = text.trim();
12            if trimmed.is_empty() {
13                Err(io::Error::new(
14                    io::ErrorKind::InvalidInput,
15                    "Input cannot be empty",
16                ))
17            } else {
18                Ok(trimmed.to_string())
19            }
20        }
21        Err(e) => {
22            Err(io::Error::new(io::ErrorKind::Other, e.to_string()))
23        }
24    }
25}