Search code examples
rustunix-socket

Using Unix sockets with socket2


I'm trying to build a Unix-domain socket using the socket2 crate and the most basic code fails to compile:

extern crate socket2;
use socket2::*;

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

This is the error:

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

The documentation indicates, that unix function is "only available on Unix when the unix feature is activated". I'm running this code on a Ubuntu machine. Do I need to activate anything else in my cargo file for this function to be enabled? The crate lacks examples that I can rely on.


Solution

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

    And How to activate a feature

    In your case just add this to your cargo manifest:

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