Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use std::io;
use std::io::Write;
fn main() {
//println! is a macro that writes a line to stdout and then flushes the
//stdout buffer
println!("The inredibly annoying Rust echo strikes again!");
//get a reference to stdin
let stdin = io::stdin();
//create a mutable string to hold what the user types.
let mut buffer = String::new();
//loop is a keyword for intentionally declaring an infinite loop.
// `while true` would give a compiler warning.
loop {
//print! is like println!, but it doesn't append a newline, and it doesn't
//flush stdout
print!(">>>");
//manually flush stdout so that ">>>" prompt appears
io::stdout().flush().expect("Could not flush stdout. Panic!");
//stdout.flush() and stdin.readline() both return a Result, which is
//an enumerated type, that is either Ok, or some error.
//the expect() call tells the compiler, basically, "on Ok do nothing,
//but on Error, fail with this message"
stdin.read_line(&mut buffer).expect("Could not read stdin. Panic!");
//echo back the user's input, because we're incredibly annoying
println!("Haha! {}", buffer.trim());
//clear the input string; only echo the most recent input.
buffer.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.