std-method

SocketAddr::set_port

Change the port of an address value before using it. This does not rebind any already-created socket. Port zero has special meaning when binding a socket and is not an ordinary service destination. Expected stdout (not executed): 127.0.0.1:9000 Common mistake: Expecting a live listener to move ports when its copied address is edited.

Formal Syntax & Structure

rust syntaxrust
addr.set_port(port: u16)

Working Example

Example Snippetrust
fn main() {    use std::net::SocketAddr;    let mut a: SocketAddr = "127.0.0.1:8080".parse().unwrap();    a.set_port(9000);    println!("{a}");}
Rust Reference