No function or associated item named `from_abstract_namespace` found for struct `std::os::unix::net::SocketAddr` in the current scope

#![allow(unused)]
#![feature(unix_socket_abstract)]
use std::os::unix::net::{UnixListener, SocketAddr};

fn main() -> std::io::Result<()> {
    let addr = SocketAddr::from_abstract_namespace(b"hidden")?;
    let listener = match UnixListener::bind_addr(&addr) {
        Ok(sock) => sock,
        Err(err) => {
            println!("Couldn't bind: {err:?}");
            return Err(err);
        }
    };
    Ok(())
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.30s
     Running `target/debug/playground`

Everything compiles on the playground, but locally I get an error

error[E0599]: no function or associated item named `from_abstract_namespace` found for struct `std::os::unix::net::SocketAddr` in the current scope
 --> src/main.rs:6:28
  |
6 |     let addr = SocketAddr::from_abstract_namespace(b"hidden")?;
  |                            ^^^^^^^^^^^^^^^^^^^^^^^
  |                            |
  |                            function or associated item not found in `std::os::unix::net::SocketAddr`
  |                            help: there is an associated function with a similar name: `from_pathname`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `socket_abstract` due to previous error

Why doesn't my assembly see the standard library?

Your Rust version is probably too old.

i

nfo: using existing install for 'nightly-x86_64-apple-darwin'
info: override toolchain for '/Users/tatyanasorokina/rust/socket_abstract' set to 'nightly-x86_64-apple-darwin'

  nightly-x86_64-apple-darwin unchanged - rustc 1.65.0-nightly (b44197abb 2022-09-05)

The method does not exist on Mac.

Yes, the method is for Linux, but can't it be compiled into a build? Besides, the error indicates the absence of a function in the standard library - "function or associated item not found in std::os::unix::net::SocketAddr "

You need to use cfgs if you have platform specific code and need to support multiple platforms.

Essentially the function doesn't exist unless the target_os is Linux, so your code that requires it also should not exist unless the target os matches.

The way platform-specific functions work is that conditional compilation is used to make them disappear on other platforms, so your error message is consistent with that being the issue.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.