Primitive std::u16

u16
Expand description
16-bit unsigned integer

Implementations

fn is_zero(self) -> bool

Returns whether a u16 is set to zero.

Returns

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

Examples

fn foo() {
    let zero_u16 = u16::zero();
    assert(zero_u16.is_zero());
}

fn try_as_u8(self) -> Option<u8>

Attempts to convert the u16 value into a u8 value.

Additional Information

The max value a u8 can represent is 255.

Returns

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

Examples

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

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

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

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

Returns

  • [[u8; 2]] - An array of 2 u8 bytes that compose the u16.

Examples

fn foo() {
    let x: u16 = 513;
    let result = x.to_le_bytes();

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

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

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

Arguments

  • bytes: [[u8; 2]] - A sequence of 2 u8 bytes that represent a u16.

Returns

  • [u16] - The resulting u16 value.

Examples

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

    assert(result == 513_u16);
}

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

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

Returns

  • [[u8; 2]] - An array of 2 u8 bytes that compose the u16.

Examples

fn foo() {
    let x: u16 = 513;
    let result = x.to_be_bytes();

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

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

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

Arguments

  • bytes: [[u8; 2]] - A sequence of 2 u8 bytes that represent a u16.

Returns

  • [u16] - The resulting u16 value.

Examples

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

    assert(result == 513_u16);
}

fn to_le_bytes(self) -> Bytes

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

Returns

  • [Bytes] - The 2 bytes that compose the u16.

Examples

fn foo() {
    let x: u16 = 513;
    let result = x.to_le_bytes();

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

fn from_le_bytes(bytes: Bytes) -> Self

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

Arguments

  • bytes: [Bytes] - The 2 bytes that compose the u16.

Returns

  • [u16] - The resulting u16 value.

Examples

use std::bytes::Bytes;

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

    assert(result == 513_u16);
}

fn to_be_bytes(self) -> Bytes

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

Returns

  • [Bytes] - The 2 bytes that compose the u16.

Examples

use std::bytes_conversions::u16;

fn foo() {
    let x: u16 = 513;
    let result = x.to_be_bytes();

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

fn from_be_bytes(bytes: Bytes) -> Self

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

Arguments

  • bytes: [Bytes] - The 2 bytes that compose the u16.

Returns

  • [u16] - The resulting u16 value.

Examples

use std::bytes::Bytes;

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

    assert(result == 513_u16);
}

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 u16.

Returns

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

Examples


fn foo() {
    let u16_value = u16::from(0u8);
}

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

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,
)