Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
Last active November 14, 2016 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willmurphyscode/f25eb5a220acdf3c354ac8d81f3ad664 to your computer and use it in GitHub Desktop.
Save willmurphyscode/f25eb5a220acdf3c354ac8d81f3ad664 to your computer and use it in GitHub Desktop.
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