Function nix::unistd::gethostname [−][src]
pub fn gethostname(buffer: &mut [u8]) -> Result<&CStr>
Get the host name and store it in the provided buffer, returning a pointer
the CStr
in that buffer on success (see
gethostname(2)).
This function call attempts to get the host name for the running system and store it in a provided buffer. The buffer will be populated with bytes up to the length of the provided slice including a NUL terminating byte. If the hostname is longer than the length provided, no error will be provided. The posix specification does not specify whether implementations will null-terminate in this case, but the nix implementation will ensure that the buffer is null terminated in this case.
use nix::unistd; let mut buf = [0u8; 64]; let hostname_cstr = unistd::gethostname(&mut buf).expect("Failed getting hostname"); let hostname = hostname_cstr.to_str().expect("Hostname wasn't valid UTF-8"); println!("Hostname: {}", hostname);