std-type

SocketAddr: IP and port

Combine an IP address with a transport port. A socket address itself does not encode TCP versus UDP. IPv6 text endpoints need brackets around the address. Expected stdout (not executed): true 8000 true Common mistake: Concatenating an IPv6 string and port without delimiters.

Formal Syntax & Structure

rust syntaxrust
SocketAddr::new(ip: IpAddr, port: u16)

Working Example

Example Snippetrust
fn main() {    use std::net::SocketAddr;    let addr: SocketAddr = "[::1]:8000".parse().unwrap();    println!("{} {}", addr.is_ipv6(), addr.port());    println!("{}", "::1:8000".parse::<SocketAddr>().is_err());}
Rust Reference