Rust htons and ntohs

I have a program that relied on native::io::net::{htons, ntohs}

, but now the errors are on Could not find 'io' in 'packet::native'

. It seems like this change happened last week.

Searching does not provide much information about the change, and searching htons

or ntohs

in documents does not provide any useful information.

What's the (new?) Standard way of doing htons

or ntohs

in Rust?

The obvious solution would be to write my own, but one would expect it to be in the standard library.

+3


source to share


2 answers


They are now in std::sys_common::net

, but std::sys_common

closed. But their implementation is src/libstd/sys/common/net.rs

very simple:



pub fn htons(u: u16) -> u16 {
    u.to_be()
}
pub fn ntohs(u: u16) -> u16 {
    Int::from_be(u)
}

      

+2


source


You can use functions from_be

and to_be

features Int

. The network order is big endian, so it is equivalent.

You can find usage examples in the documentation for these:



http://doc.rust-lang.org/core/num/trait.Int.html#method.to_be

http://doc.rust-lang.org/core/num/trait.Int.html#method.from_be

+3


source







All Articles