### Get Hostname Example Source: https://docs.rs/gethostname/1.1.0/src/gethostname/lib.rs.html Demonstrates how to use the `gethostname` function to retrieve and print the system's hostname. This is the primary usage example for the crate. ```rust use gethostname::gethostname; println!("Hostname: {:?}", gethostname()); ``` -------------------------------- ### Test: Compare with System Hostname Source: https://docs.rs/gethostname/1.1.0/src/gethostname/lib.rs.html This test verifies that the `gethostname` function returns the same value as the system's `hostname` command (or `uname -n` on non-Windows systems). It executes an external command to get the system's hostname. ```rust let mut command = if cfg!(windows) { Command::new("hostname") } else { let mut uname = Command::new("uname"); uname.arg("-n"); uname }; let output = command.output().expect("failed to get hostname"); ``` -------------------------------- ### gethostname() Source: https://docs.rs/gethostname/1.1.0/gethostname/fn.gethostname.html Retrieves the standard host name for the current machine. On Unix, it uses `rustix::system::uname` to get the node name. On Windows, it calls `GetComputerNameExW` with `ComputerNamePhysicalDnsHostname`. Be aware of a potential race condition on Windows. ```APIDOC ## Function gethostname ### Description Get the standard host name for the current machine. On Unix call `rustix::system::uname` to obtain the node name, see `rustix::system::Uname::nodename`. On Windows return the DNS host name of the local computer, as returned by GetComputerNameExW with `ComputerNamePhysicalDnsHostname` as `NameType`. We call this function twice to obtain the appropriate buffer size; there is a race condition window between these two calls where a change to the node name would result in a wrong buffer size which could cause this function to panic. Note that this host name does not have a well-defined meaning in terms of network name resolution. Specifically, it’s not guaranteed that the returned name can be resolved in any particular way, e.g. DNS. ### Signature ```rust pub fn gethostname() -> OsString ``` ``` -------------------------------- ### Get Hostname in Rust Source: https://docs.rs/gethostname/1.1.0/index.html Use the `gethostname` function from the `gethostname` crate to retrieve the hostname. This function is available for all supported platforms. ```rust use gethostname::gethostname; println!("Hostname: {:?}", gethostname()); ``` -------------------------------- ### Test Hostname Retrieval and Comparison Source: https://docs.rs/gethostname/1.1.0/src/gethostname/lib.rs.html This test verifies that the gethostname function correctly retrieves the system's hostname and compares it against the output of an external command. It handles potential errors during command execution and ensures the retrieved hostname is not empty. ```rust if output.status.success() { let hostname = String::from_utf8_lossy(&output.stdout); assert!( !hostname.is_empty(), "Failed to get hostname: hostname empty?" ); // Convert both sides to lowercase; hostnames are case-insensitive // anyway. assert_eq!( super::gethostname().into_string().unwrap().to_lowercase(), hostname.trim_end().to_lowercase() ); } else { panic!( "Failed to get hostname! {}", String::from_utf8_lossy(&output.stderr) ); } ``` -------------------------------- ### gethostname() function Source: https://docs.rs/gethostname/1.1.0/index.html Retrieves the standard host name for the current machine. ```APIDOC ## Function: gethostname ### Description Get the standard host name for the current machine. ### Usage ```rust use gethostname::gethostname; println!("Hostname: {:?}", gethostname()); ``` ### Return Value Returns the hostname as a `String`. ``` -------------------------------- ### gethostname() Source: https://docs.rs/gethostname/1.1.0/gethostname Retrieves the standard host name for the current machine. This function is available for all platforms supported by the crate. ```APIDOC ## Function: gethostname ### Description Get the standard host name for the current machine. ### Usage ```rust use gethostname::gethostname; println!("Hostname: {:?}", gethostname()); ``` ### Return Value Returns the hostname as a `Cow<'_, str>`. ``` -------------------------------- ### gethostname() Function Source: https://docs.rs/gethostname/1.1.0/gethostname/index.html Retrieves the standard host name for the current machine. This function is available for all supported platforms. ```APIDOC ## Function: gethostname ### Description Get the standard host name for the current machine. ### Usage ```rust use gethostname::gethostname; println!("Hostname: {:?}", gethostname()); ``` ### Returns The hostname of the current machine as a `Cow<'_, str>`. ``` -------------------------------- ### Windows Hostname Retrieval Source: https://docs.rs/gethostname/1.1.0/src/gethostname/lib.rs.html On Windows, this function retrieves the DNS hostname using `GetComputerNameExW` with `ComputerNamePhysicalDnsHostname`. It handles buffer sizing and potential race conditions. ```rust pub const COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME: i32 = 5; ::windows_link::link!("kernel32.dll" "system" fn GetComputerNameExW(nametype: i32, lpbuffer: *mut u16, nsize: *mut u32) -> i32); let mut buffer_size: u32 = 0; unsafe { GetComputerNameExW( COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME, std::ptr::null_mut(), &mut buffer_size, ) }; assert!(0 < buffer_size, "GetComputerNameExW did not provide buffer size"); let mut buffer = vec![0_u16; buffer_size as usize]; unsafe { if GetComputerNameExW( COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME, buffer.as_mut_ptr(), &mut buffer_size, ) == 0 { panic!("GetComputerNameExW failed to read hostname."); } } assert!(buffer_size as usize == buffer.len() - 1, "GetComputerNameExW changed the buffer size unexpectedly"); let end = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len()); OsString::from_wide(&buffer[0..end]) ``` -------------------------------- ### Unix Hostname Retrieval Source: https://docs.rs/gethostname/1.1.0/src/gethostname/lib.rs.html On Unix-like systems, this function retrieves the hostname by calling `rustix::system::uname` and extracting the nodename. Ensure `rustix` is available for Unix targets. ```rust use std::os::unix::ffi::OsStringExt; OsString::from_vec(rustix::system::uname().nodename().to_bytes().to_vec()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.