Since big-endian and little-endian have to do with byte order, and since one u8
is one byte, wouldn't u8::from_be_bytes
and u8::from_le_bytes
always have the same behavior?
Yes, they have the same behavior. The byte-oriented functions (swap_bytes
and (from|to)_[bln]e(_bytes)?
) on u8
are provided for consistency with the larger integers, even though they have trivial implementations.
Among other things, this makes it easier to write macro code that is correct for all sizes of integer, rather than having to special-case u8
.
u8
would still need to implement these functions. In its case, trivially. –
Bowen On a byte-level there is no difference. To better understand how Big-endian differs from Little-endian, consider this:
As can be seen, we have three bytes in the example, the bits of each having a different color. Notice how the bits in each byte look exactly the same in both BE and LE.
That is language-agnostic BTW.
As for the Rust functions operating on u8
, Trent explained it very well. My answer focuses more on the part how BE/LE work in general.
© 2022 - 2024 — McMap. All rights reserved.
..le_bytes
is obviously the french version – Brabble