examples/get-random-free-port/src/main.rs
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
println!("address: {:?}", listener);
println!("port: {}", listener.local_addr().unwrap().port());
}
This is being used in the tests of the meet-os project.