Text of rust.docs.kernel.org › next › core › num › struct.Wrapping.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Wrapping in core::num - Rust

Skip to main content Wrapping core :: num Struct Wrapping 1.6.0 · Source #[repr(transparent)] pub struct Wrapping<T>(pub T); Expand description Provides intentionally-wrapped arithmetic on T . Operations like + on u32 values are intended to never overflow, and in some debug configurations overflow is detected and results in a panic. While most arithmetic falls into this category, some code explicitly expects and relies upon modular arithmetic (e.g., hashing).

Wrapping arithmetic can be achieved either through methods like wrapping_add , or through the Wrapping<T> type, which says that all standard arithmetic operations on the underlying value are intended to have wrapping semantics. The underlying value can be retrieved through the .0 index of the Wrapping tuple. § Examples use std::num::Wrapping; let zero = Wrapping( 0u32 ); let one = Wrapping( 1u32 ); assert_eq! (u32::MAX, (zero - one).

0 ); § Layout Wrapping<T> is guaranteed to have the same layout and ABI as T . Tuple Fields § § 0: T Implementations § Source § impl Wrapping < usize > Source pub const MIN : Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the smallest value that can be represented by this integer type. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; assert_eq! (<Wrapping<usize>>::MIN, Wrapping(usize::MIN)); Source pub const MAX : Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the largest value that can be represented by this integer type. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; assert_eq! (<Wrapping<usize>>::MAX, Wrapping(usize::MAX)); Source pub const BITS : u32 = usize::BITS 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the size of this integer type in bits. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; assert_eq! (<Wrapping<usize>>::BITS, usize::BITS); Source pub const fn count_ones (self) -> u32 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the number of ones in the binary representation of self . § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n = Wrapping( 0b01001100usize ); assert_eq! (n.count_ones(), 3 ); Source pub const fn count_zeros (self) -> u32 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the number of zeros in the binary representation of self . § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; assert_eq! (Wrapping(!

0usize ).count_zeros(), 0 ); Source pub const fn trailing_zeros (self) -> u32 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the number of trailing zeros in the binary representation of self . § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n = Wrapping( 0b0101000usize ); assert_eq! (n.trailing_zeros(), 3 ); Source pub const fn rotate_left (self, n: u32 ) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Shifts the bits to the left by a specified amount, n , wrapping the truncated bits to the end of the resulting integer.

Please note this isn’t the same operation as the << shifting operator! § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n: Wrapping<i64> = Wrapping( 0x0123456789ABCDEF ); let m: Wrapping<i64> = Wrapping(- 0x76543210FEDCBA99 ); assert_eq! (n.rotate_left( 32 ), m); Source pub const fn rotate_right (self, n: u32 ) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Shifts the bits to the right by a specified amount, n , wrapping the truncated bits to the beginning of the resulting integer.

Please note this isn’t the same operation as the >> shifting operator! § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n: Wrapping<i64> = Wrapping( 0x0123456789ABCDEF ); let m: Wrapping<i64> = Wrapping(- 0xFEDCBA987654322 ); assert_eq! (n.rotate_right( 4 ), m); Source pub const fn swap_bytes (self) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Reverses the byte order of the integer. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n: Wrapping<i16> = Wrapping( 0b0000000_01010101 ); assert_eq! (n, Wrapping( 85 )); let m = n.swap_bytes(); assert_eq! (m, Wrapping( 0b01010101_00000000 )); assert_eq! (m, Wrapping( 21760 )); 1.37.0 (const: 1.37.0) · Source pub const fn reverse_bits (self) -> Self Reverses the bit pattern of the integer. § Examples Please note that this example is shared among integer types, which is why i16 is used.

Basic usage: use std::num::Wrapping; let n = Wrapping( 0b0000000_01010101i16 ); assert_eq! (n, Wrapping( 85 )); let m = n.reverse_bits(); assert_eq! (m. 0 as u16, 0b10101010_00000000 ); assert_eq! (m, Wrapping(- 22016 )); Source pub const fn from_be (x: Self) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Converts an integer from big endian to the target’s endianness. On big endian this is a no-op.

On little endian the bytes are swapped. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n = Wrapping( 0x1Ausize ); if cfg! (target_endian = "big" ) { assert_eq! (<Wrapping<usize>>::from_be(n), n) } else { assert_eq! (<Wrapping<usize>>::from_be(n), n.swap_bytes()) } Source pub const fn from_le (x: Self) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Converts an integer from little endian to the target’s endianness.

On little endian this is a no-op. On big endian the bytes are swapped. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n = Wrapping( 0x1Ausize ); if cfg! (target_endian = "little" ) { assert_eq! (<Wrapping<usize>>::from_le(n), n) } else { assert_eq! (<Wrapping<usize>>::from_le(n), n.swap_bytes()) } Source pub const fn to_be (self) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Converts self to big endian from the target’s endianness.

On big endian this is a no-op. On little endian the bytes are swapped. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n = Wrapping( 0x1Ausize ); if cfg! (target_endian = "big" ) { assert_eq! (n.to_be(), n) } else { assert_eq! (n.to_be(), n.swap_bytes()) } Source pub const fn to_le (self) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Converts self to little endian from the target’s endianness.

On little endian this is a no-op. On big endian the bytes are swapped. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; let n = Wrapping( 0x1Ausize ); if cfg! (target_endian = "little" ) { assert_eq! (n.to_le(), n) } else { assert_eq! (n.to_le(), n.swap_bytes()) } Source pub fn pow (self, exp: u32 ) -> Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Raises self to the power of exp , using exponentiation by squaring. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; assert_eq! (Wrapping( 3usize ).pow( 4 ), Wrapping( 81 )); Results that are too large are wrapped: #![feature(wrapping_int_impl)] use std::num::Wrapping; assert_eq! (Wrapping( 3i8 ).pow( 5 ), Wrapping(- 13 )); assert_eq! (Wrapping( 3i8 ).pow( 6 ), Wrapping(- 39 )); Source § impl Wrapping < u8 > Source pub const MIN : Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the smallest value that can be represented by this integer type. § Examples Basic usage: #![feature(wrapping_int_impl)] use std::num::Wrapping; assert_eq! (<Wrapping<u8>>::MIN, Wrapping(u8::MIN)); Source pub const MAX : Self 🔬 This is a nightly-only experimental API. ( wrapping_int_impl #32463 ) Returns the largest value that can be represented by this

The rest of this page is on the live site.