Repository navigation
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.
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
- ACP: Add missing integer casts libs-team#788
- Implementation: Implement feature
integer_casts#157402 - Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- What should happen to the current unstable narrow/truncate API?
- Should the
NonZerotypes be included?
Footnotes
Reacted by blep, George Tsiamasiotis, Techcable, Gabriel Bjørnager Jensen and Alex
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.
Activity
orlp commentedon Jun 3, 2026
I can't assign myself but I will try to port the implementation from the
int-castcrate to std this week.Rollup merge of rust-lang#157402 - orlp:int-cast, r=jhpratt
Rollup merge of rust-lang#157402 - orlp:int-cast, r=jhpratt
Auto merge of #157402 - orlp:int-cast, r=jhpratt
Auto merge of #157402 - orlp:int-cast, r=jhpratt
Auto merge of #157402 - orlp:int-cast, r=jhpratt
Rollup merge of rust-lang#157402 - orlp:int-cast, r=jhpratt
Unrolled build for #157402
Apersoma commentedon Jun 9, 2026
The
BoundedCastFromInttrait would need to be split into a saturating and wrapping variants forNonZero<uint>to ever be supported by the saturating cast since you can't implement the wrapping cast for it (except for converting between signed and unsigned nonzeros of the same size butcast_signedandcast_unsignedalready exist).orlp commentedon Jun 9, 2026
@Apersoma I would never implement a wrapping variant for
NonZeroas it's unclear what it means. Saturating somewhat makes sense forNonZero<uint>but makes no sense forNonZero<int>.Personally I think it's better if we implement neither operation with
NonZeroas the output type.NonZerois fine as source type forBoundedCastFromInt, and always fine forCheckedCastFromIntboth as source and target.bgkillas commentedon Jun 11, 2026
asking if we can extend support to booleans so we dont still need to do
u8::from(val).strict_cast::<i32>()instead ofval as i32, so we just haveval.strict_cast::<i32>()wherevalis a boolorlp commentedon Jun 11, 2026
@bgkillas
i32::from(val)?bgkillas commentedon Jun 11, 2026
dont know why i didn't think we had this im silly, still feel like for consistency it would be nice to have the applicable cast functions for bool but idk.
Rollup merge of #157402 - orlp:int-cast, r=jhpratt
orlp commentedon Jun 11, 2026
Rollup merge of #157402 - orlp:int-cast, r=jhpratt