$ curl cheat.sh/
/*
 * As other people have said, make sure to read the [error handling
 * chapter](https:doc.rust-lang.org/stable/book/ch09-00-error-
 * handling.html).
 * 
 * In most cases, you don't want to use `println!` to report errors.
 * Either you should return the error from your function and let the
 * caller deal with it, or you should use `panic!` to abort that thread
 * and potentially the process.
 * 
 * ```
 * match io::stdout().flush() {
 */
 Ok(_) => print!(""),
 Err(error) => println!("{}", error),
/*
 * }
 * ```
 * 
 * Instead of printing nothing (which is inefficient), you can just... do
 * nothing:
 * 
 * ```
 * match io::stdout().flush() {
 */
 Ok(_) => (),
 Err(error) => println!("{}", error),
/*
 * }
 * ```
 * 
 * Since you don't care about the success case, you can use an `if let`:
 * 
 * ```
 * if let Err(error) = io::stdout().flush() {
 */
 println!("{}", error);
/*
 * }
 * ```
 * 
 * Replacing the `println` with a `panic!` would be even better:
 * 
 * ```
 * if let Err(error) = io::stdout().flush() {
 */
 panic!("{}", error);
/*
 * }
 * ```
 * 
 * This is almost exactly what [`Option::unwrap`](https:doc.rust-
 * lang.org/std/option/enum.Option.html#method.unwrap) does
 * ([source](https:github.com/rust-
 * lang/rust/blob/1.34.1/src/libcore/option.rs#L342-L347)), except it
 * also returns the successful value when present:
 * 
 * ```
 * pub fn unwrap(self) -> T {
 */
 match self {
     Some(val) => val,
     None => panic!("called `Option::unwrap()` on a `None` value"),
 }
/*
 * }
 * ```
 * 
 * However, it's **even better** to use
 * [`Option::expect`](https:doc.rust-
 * lang.org/std/option/enum.Option.html#method.expect) which allows you
 * to specify an additional error message:
 * 
 * ```
 * io::stdout().flush().expect("Unable to flush stdout");
 * ```
 * 
 * Applying that twice:
 * 
 * ```
 * use std::io::{self, Write};
 * 
 * fn main() {
 */
 print!("What is your name? ");

 io::stdout().flush().expect("Unable to flush stdout");

 let mut name = String::new();
 io::stdin()
     .read_line(&mut name)
     .expect("Unable to read the line");

 let name = name.trim();

 if !name.is_empty() {
     println!("Hello, {}, nice to meet you!", name);
 } else {
     println!("No name entered, goodbye.");
 }
/*
 * }
 * ```
 * 
 * Note that there's no need to re-allocate a `String`, you can just
 * shadow `name`, and there's no need to use `format` just to print out
 * stuff.
 * 
 * Since Rust 1.26.0, you could also choose to return a `Result` from
 * `main`:
 * 
 * ```
 * use std::io::{self, Write};
 * 
 * fn main() -> Result<(), io::Error> {
 */
 print!("What is your name? ");
 io::stdout().flush()?;

 let mut name = String::new();
 io::stdin().read_line(&mut name)?;

 let name = name.trim();

 if !name.is_empty() {
     println!("Hello, {}, nice to meet you!", name);
 } else {
     println!("No name entered, goodbye.");
 }

 Ok(())
/*
 * }
 * ```
 * 
 * > but I could write this shorter in C, after all...
 * 
 * I would encourage / challenge you to attempt this. Note that every
 * memory allocation in this program is checked, as is every failure case
 * dealing with the standard output. Many people are not aware that C's
 * `printf` **returns an error code** that you should be checking. Try
 * outputting to a pipe that has been closed for an example.
 * 
 * [Shepmaster] [so/q/39174864] [cc by-sa 3.0]
 */

$
Follow @igor_chubin cheat.sh