Open
Description
What it does
cast_signed
and cast_unsigned
are stable since 1.87.
They produce the same result as an as
cast, but they ensure that the bit-width remains the same.
Thus, for projects that may want to avoid as
casts as much as possible, using cast_{un,}signed
may be positive.
Advantage
Reduce the need for as
casts.
Drawbacks
No response
Example
u32::MAX as i32
-1i32 as u32
Could be written as:
u32::MAX.cast_signed()
(-1i32).cast_unsigned()