Get random free port in Rust

TcpListener bind local_addr port

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.

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo