List of panic cases

I can't find a comprehensive list of panic cases on the language level. As far as I know the language panic:

  • if you willingly raise a panic
  • if you divide by 0
  • if you overflow an integer when overflow checking is enabled
  • if you index an array out of bounds

Is there a comprehensive list of the cases where the language itself can cause a panic? I'm not talking about the panic in the standard library, I know it can panic in a lot of places. (allocations failure, invalid parameters, ...)

Bounds checks, division by zero, etc. are all ultimately implemented as panic!() or equivalent. Given that large number of things that can go wrong, I'd imagine you'd be hard pressed to find an exhaustive list. There are things like debug_assert!() that shouldn't panic, but still technically could.

That's why I specified 'at the language level'. I would like to know when a code that does not call any function or macro from the standard library can fail.

You explicitly excluded the standard library. That would rule out indexing out of bounds, overflowing integer arithmetic, and division by zero. They are implemented in the standard library, hence my confusion.

Yes of course I consider intrinsics are part of the language, even if they are handled in the standard library. It make no sense to consider the language without it.

It sounds like there are two categories of panic cases here.

In the first category you have "intrinsics"; panics which don't have any associated source code because the compiler provides the implementation. There actually aren't many of these because a lot of the time using an intrinsic incorrectly just results in UB.

  • divide-by-zero (for numbers, the / operator is known specially by the compiler so the std::ops::Div impl defers to itself)
  • integer overflow

In the second category are checks written in the standard library that ensure intrinsics are used correctly or invariants are upheld (index out of bounds, valid utf-8, assertions against programming error, etc.).

You've said you don't care about these cases, but you should still be able to find most of them by just grepping for things like panic!(), unreachable!(), assert!(), unwrap(), and expect() in the Rust repo's library/ directory.


I care about panic in the standard library, but as you say, I know that I can find out about them by inspecting the functions I call. They are usually documented too.

What I would like to know if there is an exhaustive list of the language intrinsics that can rise a panic, since I can't find them that easily.


There was a recent discussion on LKML related to Rust and panics and Linux kernel (although primarily from memory allocation perspective): LKML: Linus Torvalds: Re: [PATCH 00/13] [RFC] Rust support (this is just a single post but there's more the thread.)

I love it.

"...on the whole I don't hate it."

Linus Torvalds on Rust. Perhaps this should be a quote of the week.