gpt4 book ai didi

Rust htons 和 ntohs

转载 作者:行者123 更新时间:2023-11-29 07:57:52 27 4
gpt4 key购买 nike

我有一个程序一直依赖于 native::io::net::{htons, ntohs} 但现在在 Could not find 'io' in 'packet: :native'。变化似乎发生在上周的某个时间

搜索不会产生太多关于更改的信息,并且在文档中搜索 htonsntohs 也不会产生任何有用的信息。

在 Rust 中执行 htonsntohs 的(新的?)标准方法是什么?

一个明显的解决方案是自己编写,但人们希望它会在标准库中。

最佳答案

这些现在在 std::sys_common::net 中,但是 std::sys_common 是私有(private)的。但是它们在 src/libstd/sys/common/net.rs 中的实现非常简单:

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

关于Rust htons 和 ntohs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27029083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com