In Rust, the panic! macro is used to abort the program and provide an error message. It’s typically used when something unexpected happens that the program can’t handle.
Running this program will cause it to panic, print an error message and backtrace, and exit with a non-zero status.
When the first panic! in main fires, the program exits without reaching the rest of the code. If you’d like to see the program try to create a temp file, comment out the first panic!.
If you run with the RUST_BACKTRACE=1 environment variable, you’ll see a more detailed backtrace:
Note that unlike some languages which use exceptions for handling of many errors, in Rust it is idiomatic to use the Result type for recoverable errors and reserve panic! for unrecoverable errors.