Primitive std::u32

u32
Expand description
32-bit unsigned integer

Implementations

fn is_zero(self) -> bool

Returns whether a u32 is set to zero.

Returns

  • [bool] -> True if the u32 is zero, otherwise false.

Examples

fn foo() {
    let zero_u32 = u32::zero();
    assert(zero_u32.is_zero());
}

fn try_as_u8(self) -> Option<u8>

Attempts to convert the u32 value into a u8 value.

Additional Information

The max value a u8 can represent is 255.

Returns

[Option] - Some(u8) if the u32 is less than or equal to the max u8 value. Else None.

Examples

fn foo() {
    let val = 255_u32.try_as_u8();
    assert(val == Some(255_u8));

    // Conversion fails as value is above the max a u8 can represent.
    let val2 = 256_u32.try_as_u8();
    assert(val == None);
}

fn try_as_u16(self) -> Option<u16>

Attempts to convert the u32 value into a u16 value.

Additional Information

The max value a u16 can represent is 65_535.

Returns

[Option] - Some(u16) if the u32 is less than or equal to the max u16 value. Else None.

Examples

fn foo() {
    let val = 65_535_u32.try_as_u16();
    assert(val == Some(65_535_u16));

    // Conversion fails as value is above the max a u16 can represent.
    let val2 = 65_536_u32.try_as_u16();
    assert(val == None);
}

fn to_le_bytes(self) -> [u8; 4]

Converts the u32 to a sequence of little-endian bytes.

Returns

  • [[u8; 4]] - An array of 4 u8 bytes that compose the u32.

Examples

fn foo() {
    let x: u32 = 67305985;
    let result = x.to_le_bytes();

    assert(result[0] == 1_u8);
    assert(result[1] == 2_u8);
    assert(result[2] == 3_u8);
    assert(result[3] == 4_u8);
}

fn from_le_bytes(bytes: [u8; 4]) -> Self

Converts a sequence of little-endian bytes to a u32.

Arguments

  • bytes: [[u8; 4]] - A sequence of 4 u8 bytes that represent a u32.

Returns

  • [u32] - The resulting u32 value.

Examples

fn foo() {
    let bytes = [1_u8, 2_u8, 3_u8, 4_u8];
    let result = u32::from_le_bytes(bytes);

    assert(result == 67305985_u32);
}

fn to_be_bytes(self) -> [u8; 4]

Converts the u32 to a sequence of big-endian bytes.

Returns

  • [[u8; 4]] - An array of 4 u8 bytes that compose the u32.

Examples

fn foo() {
    let x: u32 = 67305985;
    let result = x.to_be_bytes();

    assert(result[0] == 4_u8);
    assert(result[1] == 3_u8);
    assert(result[2] == 2_u8);
    assert(result[3] == 1_u8);
}

fn from_be_bytes(bytes: [u8; 4]) -> Self

Converts a sequence of big-endian bytes to a u32.

Arguments

  • bytes: [[u8; 4]] - A sequence of 4 u8 bytes that represent a u32.

Returns

  • [u32] - The resulting u32 value.

Examples

fn foo() {
    let bytes = [4_u8, 3_u8, 2_u8, 1_u8];
    let result = u32::from_be_bytes(bytes);

    assert(result == 67305985_u32);
}

fn to_le_bytes(self) -> Bytes

Converts the u32 to a sequence of little-endian bytes.

Returns

  • [Bytes] - The 4 bytes that compose the u32.

Examples

use std::bytes_conversions::u32::*;

fn foo() {
    let x: u32 = 67305985;
    let result = x.to_le_bytes();

    assert(result.get(0).unwrap() == 1_u8);
    assert(result.get(1).unwrap() == 2_u8);
    assert(result.get(2).unwrap() == 3_u8);
    assert(result.get(3).unwrap() == 4_u8);
}

fn from_le_bytes(bytes: Bytes) -> Self

Converts a sequence of little-endian bytes to a u32.

Arguments

  • bytes: [Bytes] - The 4 bytes that compose the u32.

Returns

  • [u32] - The resulting u32 value.

Examples

use std::{bytes::Bytes, bytes_conversions::u32::*};

fn foo() {
    let mut bytes = Bytes::new();
    bytes.push(1_u8);
    bytes.push(2_u8);
    bytes.push(3_u8);
    bytes.push(4_u8);
    let result = u32::from_le_bytes(bytes);

    assert(result == 67305985_u32);
}

fn to_be_bytes(self) -> Bytes

Converts the u32 to a sequence of big-endian bytes.

Returns

  • [Bytes] - The 4 bytes that compose the u32.

Examples

use std::bytes_conversions::u32::*;

fn foo() {
    let x: u32 = 67305985;
    let result = x.to_be_bytes();

    assert(result.get(0).unwrap() == 4_u8);
    assert(result.get(1).unwrap() == 3_u8);
    assert(result.get(2).unwrap() == 2_u8);
    assert(result.get(3).unwrap() == 1_u8);
}

fn from_be_bytes(bytes: Bytes) -> Self

Converts a sequence of big-endian bytes to a u32.

Arguments

  • bytes: [Bytes] - The 4 bytes that compose the u32.

Returns

  • [u32] - The resulting u32 value.

Examples

use std::{bytes::Bytes, bytes_conversions::u32::*};

fn foo() {
    let mut bytes = Bytes::new();
    bytes.push(4_u8);
    bytes.push(3_u8);
    bytes.push(2_u8);
    bytes.push(1_u8);
    let result = u32::from_be_bytes(bytes);

    assert(result == 67305985_u32);
}

Trait Implementations

fn sqrt(self) -> Self

fn pow(self, exponent: u32) -> Self

fn log(self, base: Self) -> Self

fn log2(self) -> Self

fn from(u: u8) -> Self

Casts a u8 to a u32.

Returns

  • [u32] - The u32 representation of the u8 value.

Examples


fn foo() {
    let u32_value = u32::from(0u8);
}

fn from(u: u16) -> Self

Casts a u16 to a u32.

Returns

  • [u32] - The u32 representation of the u16 value.

Examples


fn foo() {
    let u32_value = u32::from(0u16);
}

fn try_from(u: u64) -> Option<Self>

fn try_from(u: u256) -> Option<Self>

fn try_from(u: U128) -> Option<Self>

fn hash(
self,
refmut state: Hasher,
)