blob: 93880dea0972cf917b10800010905c1a2ab2b405 (
plain)
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
32
33
34
35
36
37
38
|
use std::error::Error;
use std::{io, io::Write};
fn prompt() -> Result<(), Box<dyn Error>> {
print!("> ");
io::stdout().flush()?;
Ok(())
}
fn read_stdin() -> Result<String, Box<dyn Error>> {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer)?;
Ok(buffer)
}
fn main() -> Result<(), Box<dyn Error>> {
loop {
prompt()?;
let user_input = read_stdin()?;
if user_input.len() == 0 {
// control-d or end of input. Needs to be specially handled before
// the match because this is identical to whitespace after the trim.
break;
}
match user_input.trim() {
"" => {}
"exit" => {
break;
}
other_input => {
println!("Unknown input {}", other_input);
}
}
}
Ok(())
}
|