What is the preferred way to byte swap values in Rust?
-
22-12-2019 - |
Question
I see that before Rust 1.0, there used to be functions like std::mem::from_be16
as well as core::mem::from_be16
. The documentation for both modules claims that all of the byte swapping routines are deprecated, and indeed they were removed for Rust 1.0.
Is there anything built into Rust which can do the swap and is not deprecated or would I have to implement my own?
Solution
You want to use methods like i32::from_be
.
For future reference if you run code with deprecated functions the recommended replacement should be shown by the compiler or alternatively view the source code of the function which should have something like this
#[deprecated = "use `i32::from_be` instead"]
OTHER TIPS
Methods like u16::swap_bytes
sound more relevant to the wording of the question.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow