rust-lang / rust-lang/rust

Tracking Issue for `integer_casts`

Open
#157,388 7 comments 3 reactions 1 assignee View on GitHub

@orlp is already working on this.

Since Jun 7, 2026.

C-tracking-issue T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Feature gate: #![feature(integer_casts)]

This is a tracking issue for the addition of saturating, wrapping, checked, unchecked, and strict casts between integer types.

Public API

For each built-in integer type T the following methods are added:

impl T {
    /// Converts `self` to the target integer type, saturating at the nearest edge
    /// of the target type's domain if the value does not lie in within it.
    fn saturating_cast<T: BoundedCastFromInt<Self>>(self) -> T;
    
    /// Converts `self` to the target integer type, wrapping around at the
    /// boundary of the target type.
    fn wrapping_cast<T: BoundedCastFromInt<Self>>(self) -> T;

    /// Converts `self` to the target integer type, returning `None` if the value
    /// does not lie in the target type's domain.
    fn checked_cast<T: CheckedCastFromInt<Self>>(self) -> Option<T>;

    /// Equivalent to `self.checked_cast::<Int>().unwrap()`.
    fn strict_cast<T: CheckedCastFromInt<Self>>(self) -> T;

    /// Equivalent to `self.checked_cast::<Int>().unwrap_unchecked()`.
    unsafe fn unchecked_cast<T: CheckedCastFromInt<Self>>(self) -> T;
}
Steps / History
Unresolved Questions
  • What should happen to the current unstable narrow/truncate API?
  • Should the NonZero types be included?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.