Skip to main content

Custom error types

Once a function can fail in more than one way, `String` errors stop being enough. An enum names each failure so callers can `match` on the one they care about.

Implementing `Display` gives a human-readable message, and implementing `std::error::Error` lets your type slot into the wider ecosystem — including `Box<dyn Error>`.

This is the shape that libraries like `thiserror` generate for you. Writing it once by hand makes the generated version obvious later.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust