expression

Accept a Unix-domain connection in a scratch directory

A filesystem-named Unix socket allows local processes with suitable access to connect. Bind only inside a private scratch directory, never unlink an arbitrary existing path. The path persists after dropping the listener, so its owner should remove it. This fixture uses a single local connection; the runner supplies a temporary directory. Expected stdout (not executed): local: z Common mistake: Unlinking an existing socket in a shared directory or assuming a pathname authenticates a peer. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
UnixListener::bind(path); UnixStream::connect(path); listener.accept()

Working Example

Example Snippetrust
use std::{fs,io::{self,Read,Write},os::unix::net::{UnixListener,UnixStream}};fn main() -> io::Result<()> {    let path="demo.sock";    let listener=UnixListener::bind(path)?;    let mut client=UnixStream::connect(path)?;    let (mut server,_)=listener.accept()?;    client.write_all(b"z")?;    let mut b=[0;1]; server.read_exact(&mut b)?;    drop(server); drop(client); drop(listener); fs::remove_file(path)?;    println!("local: {}",char::from(b[0])); Ok(())}
Rust Reference