@herbert Hi Herbert, I’m a big fan of your books and a supporter on Patreon. Would you use .unwrap() or .expect() in the context of using the print! vs the
println! macro?
:
Some may not be aware, but using the print!
(w/o newline) vs println!
macro, it’s necessary many times to implement io::stdout().flush()
which will cause many beginner programmer’s eyes to glaze over. It’s probably the reason most introductory tutorials stick with the println!
macro in order not to overwhelm the beginner programmer. Example use case of printing without a newline:
use std::io;
use std::io::stdin;
use std::io::Write; // for .flush()
fn main() {
print!("What is your name? ");
io::stdout().flush().unwrap(); // or .expect("FAIL!");? here;
let mut fname = String::new();
stdin()
.read_line(&mut fname)
.expect("Failed to read line");
println!("It's nice to meet you {}!", fname.trim());
}