Native unsigned integer (machine word)
On 64-bit systems, usize
is equivalent to u64. On 32-bit systems, usize
is equivalent
to u32.
To help writing portable code, usize
is a distinct type and not just a type alias.
Conversions from usize
to the equivalently sized unsigned integer require an explicit cast.
Methods
impl usize { ... }
-
fn max_value() -> usize
Returns the maximum value of
usize
. -
fn min_value() -> usize
Returns the minimum value of
usize
.
Mixins
impl usize { ... }
-
-
Swaps the byte order (endianness) of the number.
Example
assert_eq!(0x0123u16.swap_bytes(), 0x2301);
Run this example -
Converts the number into a big-endian representation.
If the numbers of this type are represented in big-endian order on the target architecture, then this is an identity function.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.to_be(), 0x2301); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.to_be(), 0x0123);
Run this example -
Converts the number from a big-endian representation.
If the numbers of this type are represented in big-endian order on the target architecture, then this is an identity function.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.from_be(), 0x2301); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.from_be(), 0x0123);
Run this example -
Converts the number into a little-endian representation.
If the numbers of this type are represented in little-endian order on the target architecture, then this is an identity function.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.to_le(), 0x0123); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.to_le(), 0x2301);
Run this example -
Converts the number from a little-endian representation.
If the numbers of this type are represented in little-endian order on the target architecture, then this is an identity operation.
Example
#[cfg(target_endian = "little")] assert_eq!(0x0123u16.from_le(), 0x0123); #[cfg(target_endian = "big")] assert_eq!(0x0123u16.from_le(), 0x2301);
Run this example -
Returns the number of ones in the binary representation of the number.
Example
assert_eq!(0b10101000u8.count_ones(), 3);
Run this example -
Returns the number of zeros in the binary representation of the number.
Example
assert_eq!(0b10101000u8.count_zeros(), 5);
Run this example -
Returns the number of leading zeros in the binary representation of the number.
Example
assert_eq!(0b00010000u8.leading_zeros(), 3);
Run this example -
Returns the number of trailing zeros in the binary representation of the number.
Example
assert_eq!(0b00010000u8.trailing_zeros(), 4);
Run this example -
Returns the number of leading ones in the binary representation of the number.
Example
assert_eq!(0b11111110u8.leading_ones(), 7);
Run this example -
Returns the number of trailing ones in the binary representation of the number.
Example
assert_eq!(0b00000111u8.trailing_ones(), 3);
Run this example -
Rotates the bits to the left by
n
bits.Example
assert_eq!(0b01000111u8.rotate_left(2), 0b00011101u8);
Run this example -
Rotates the bits to the right by
n
bits.Example
assert_eq!(0b01000111u8.rotate_right(2), 0b11010001u8);
Run this example -
mixin WithOverflow<usize>
-
Adds two numbers, wrapping around at the boundary of the type.
-
Subtracts two numbers, wrapping around at the boundary of the type.
-
Multiplies two numbers, wrapping around at the boundary of the type.
-
Divides two numbers, wrapping around at the boundary of the type.
The only case where such wrapping can occur is when one divides
MIN / -1
on a signed type. This is equivalent to-MIN
, a number that is too large to represent in the type. In such a case, this function returnsMIN
itself. -
Tries to add two numbers, returning
Option::none()
if overflow occurrs. -
Tries to subtract two numbers, returning
Option::none()
if overflow occurrs. -
Tries to multiply two numbers, returning
Option::none()
if overflow occurrs. -
Tries to divide two numbers, returning
Option::none()
if overflow occurrs orrhs
is zero.Overflow during division can only happen when one divides
MIN / -1
on a signed type. -
mixin BuiltinComparable<usize>
-
Returns
false
if arguments are equal,true
otherwise -
Returns
true
iflhs
is strictly less thanrhs
,false
otherwise -
Returns
true
iflhs
is less or equal torhs
,false
otherwise -
Returns
true
iflhs
strictly greater thanrhs
,false
otherwise -
Returns
true
iflhs
greater than or equalrhs
,false
otherwise -
mixin<H> IntegerHashable<usize, H>
H: Hasher<H> -
fn hash(self: &Self, hasher: &mut H)
-
mixin<F> IntegerFormattable<usize, F>
F: Formatter<F> -
mixin IntegerParsable<usize>
-
Parses the integer from a base-10 string.
If the string does not represent a valid integer,
Option::none()
is returned.Example
assert_eq!(i32::parse("10"), Option::some(10)); assert_eq!(i32::parse("foo"), Option::none());
Run this example -
Parses the integer from a string in a given base.
If the string does not represent a valid integer in the given base,
Option::none()
is returned.Example
assert_eq!(i32::parse_with_radix("10", 2), Option::some(2)); assert_eq!(u64::parse_with_radix("deadbeef", 16), Option::some(0xdeadbeefu64)); assert_eq!(i32::parse_with_radix("foo", 10), Option::none());
Run this example