1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::net::TcpListener;

/// Returns a free, ready to use tcp port from operating system
/// # Examples
///
/// ```no_run
/// use netutils::tcp;
///
/// fn main() {
///     println!("Port {} is available", tcp::free_port().unwrap());
/// }
/// ```
pub fn free_port() -> Option<u16> {
    match TcpListener::bind("0.0.0.0:0") {
        Ok(addr) => match addr.local_addr() {
            Ok(addr) => Some(addr.port()),
            Err(_) => None,
        },
        Err(_) => None,
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_tcp_free_port() {
        assert_ne!(super::free_port(), None);
    }
}