core::hint::unreachable_unchecked - Rust
Description: API documentation for the Rust `unreachable_unchecked` fn in crate `core`.
Keywords: rust, rustlang, rust-lang, unreachable_unchecked

# Function core::hint::unreachable_unchecked 1.27.0[−][src]

    pub unsafe fn unreachable_unchecked() -> !

Informs the compiler that this point in the code is not reachable, enabling further optimizations.

# Safety

Reaching this function is completely undefined behavior (UB). In particular, the compiler assumes that all UB must never happen, and therefore will eliminate all branches that reach to a call to `unreachable_unchecked()`.

Like all instances of UB, if this assumption turns out to be wrong, i.e. the `unreachable_unchecked()` call is actually reachable among all possible control flow, the compiler will apply the wrong optimization strategy, and may sometimes even corrupt seemingly unrelated code, causing difficult-to-debug problems.

Use this function only when you can prove that the code will never call it.

The `unreachable!()` macro is the safe counterpart of this function, which will panic instead when executed.

# Example

    fn div_1(a: u32, b: u32) -> u32 {
        use std::hint::unreachable_unchecked;

        // `b.saturating_add(1)` is always positive (not zero),
        // hence `checked_div` will never return None.
        // Therefore, the else branch is unreachable.
        a.checked_div(b.saturating_add(1))
            .unwrap_or_else(|| unsafe { unreachable_unchecked() })
    }

    assert_eq!(div_1(7, 0), 7);
    assert_eq!(div_1(9, 1), 4);
    assert_eq!(div_1(11, std::u32::MAX), 0);Run

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)