O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Panicking

Even though Rust does not have an exception mechanism designed for general error-handling usage, panicking is not far from it. Panics are non-recoverable errors that crash your thread. If the current thread is the main one, then the whole program crashes.

On a more technical level, panicking happens in the same process as exceptions: unwinding the call stack. This means climbing up and out of the place in the code where the panicking happened till hitting the top, at which point, the thread in question aborts. Here's an example where we have two call stacks:

  • f1 spawns a new thread and calls f2, which calls f3, which panics
  • main calls f2, which calls f3, which panics

Take a look at the following code snippet:

// panic.rs use std::thread; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required