Why won't the program print "Please enter your name" before waiting for input?
Well yes. That's right, for performance reasons, standard output is buffered. Recording is completed, but it was recorded only in memory. If you want it to actually be displayed to the user, you need to call a flash. This can be done either by writing a new line or by explicitly:
io::Write::flush(&mut io::stdout()).expect("flush failed!"); // If you "use" `io::Write`... io::stdout().flush().expect("flush failed!");
Also, is it possible to βdo nothingβ if the returned result is βOKβ?
Sure. Just ... do nothing.
match io::stdin().read_line(&mut name) { Ok(_) => { }, Err(err) => println!("Could not parse input: {}", err) }
The relevant requirement here is that all hands in match must have the same type of result. In the case of println! this leads to a () ; besides an empty block (or another function returning () ), you can just use a literal:
match io::stdin().read_line(&mut name) { Ok(_) => (), Err(err) => println!("Could not parse input: {}", err) }
source share