Panics
Let's go back to the speed
function you wrote for the "Variables" section.
It probably looked something like this:
fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
let distance = end - start;
distance / time_elapsed
}
If you have a keen eye, you might have spotted one issue1: what happens if time_elapsed
is zero?
You can try it
out on the Rust playground!
The program will exit with the following error message:
thread 'main' panicked at src/main.rs:3:5:
attempt to divide by zero
This is known as a panic.
A panic is Rust's way to signal that something went so wrong that
the program can't continue executing, it's an unrecoverable error2. Division by zero classifies as such an
error.
The panic! macro
You can intentionally trigger a panic by calling the panic!
macro3:
fn main() {
panic!("This is a panic!");
// The line below will never be executed
let x = 1 + 2;
}
There are other mechanisms to work with recoverable errors in Rust, which we'll cover later. For the time being we'll stick with panics as a brutal but simple stopgap solution.
Further reading
There's another issue with speed
that we'll address soon enough. Can you spot it?
You can try to catch a panic, but it should be a last resort attempt reserved for very specific circumstances.
If it's followed by a !
, it's a macro invocation. Think of macros as spicy functions for now. We'll
cover them in more detail later in the course.
Exercise
The exercise for this section is located in 02_basic_calculator/04_panics