使用带有 socket2 的 Unix 套接字

Using Unix sockets with socket2

我正在尝试使用 socket2 crate 构建 Unix 域套接字,但最基本的代码无法编译:

extern crate socket2;
use socket2::*;

fn main() {
    let socket = Socket::new(Domain::unix(), Type::dgram(), None).unwrap();
}

这是错误:

5 | let socket = Socket::new(Domain::unix(), Type::dgram(), None).unwrap();
  |                          ^^^^^^^^^^^^ function or associated item not found in 
                                               `socket2::Domain`

documentation表示unix函数是"only available on Unix when the unix feature is activated"。我在 Ubuntu 机器上 运行 这段代码。我是否需要激活我的货物文件中的任何其他内容才能启用此功能?板条箱缺少我可以依赖的示例。

This function is only available on Unix when the unix feature is activated.

How to activate a feature

在您的情况下,只需将此添加到您的货物清单中:

[dependencies.socket2]
version = "0.3.7"
features = ["unix"]