Get random free port in Rust

TcpListener bind local_addr port


use std::net::TcpListener;

fn main() {
    let listener = TcpListener::bind("").unwrap();
    println!("address: {:?}", listener);
    println!("port: {}", listener.local_addr().unwrap().port());

This is being used in the tests of the meet-os project.


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