Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Standard Library Modules

Rust IpAddr and SocketAddr types

Reference for Rust IpAddr, Ipv4Addr, Ipv6Addr and SocketAddr: parsing, constants, classification methods, octets and converting addresses to strings.

std::net models network addresses with a small family of types. Ipv4Addr and Ipv6Addr hold one address each, IpAddr is an enum over the two, and SocketAddr pairs an IpAddr with a port number. SocketAddrV4 and SocketAddrV6 are the concrete forms; SocketAddrV6 also carries a flow label and scope id.

All of these types are small Copy values that implement Display, Debug, FromStr, Eq, Ord and Hash. They never touch the network: creating, parsing and inspecting an address is pure computation. Name resolution (turning "example.com:80" into addresses) is done by the ToSocketAddrs trait, which can block.

Rust
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

let ip = Ipv4Addr::new(192, 168, 1, 10);
let addr = SocketAddr::new(IpAddr::V4(ip), 8080);

Try it

Rust Parsing and inspecting addresses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Socket addresses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
pub enum IpAddr { V4(Ipv4Addr), V6(Ipv6Addr) }
pub struct Ipv4Addr { /* 4 octets */ }
pub struct Ipv6Addr { /* 8 segments of 16 bits */ }
pub enum SocketAddr { V4(SocketAddrV4), V6(SocketAddrV6) }

impl Ipv4Addr {
    pub const LOCALHOST: Ipv4Addr;
    pub const UNSPECIFIED: Ipv4Addr;
    pub const BROADCAST: Ipv4Addr;
    pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr;
    pub const fn octets(&self) -> [u8; 4];
}
impl SocketAddr {
    pub const fn new(ip: IpAddr, port: u16) -> SocketAddr;
    pub const fn ip(&self) -> IpAddr;
    pub const fn port(&self) -> u16;
}
Method Description
Ipv4Addr::new(a, b, c, d) Builds an address from four octets.
Ipv6Addr::new(a, ..., h) Builds an address from eight 16-bit segments.
octets(), segments() The raw bytes ([u8; 4], [u8; 16] for v6) or 16-bit groups.
is_loopback(), is_unspecified(), is_multicast() Classify an address on both v4 and v6 types and on IpAddr.
is_private(), is_link_local(), is_broadcast() Extra checks available on Ipv4Addr.
to_canonical() Turns an IPv4-mapped IPv6 address into its IPv4 form; other addresses are unchanged.
SocketAddr::ip(), port() Accessors; set_ip and set_port change them in place.

Errors: str::parse returns AddrParseError when the text is not a valid address. A SocketAddr needs a port, and IPv6 addresses must be written in brackets ([::1]:80).

Usage notes

  • Constants are const, so Ipv4Addr::LOCALHOST (127.0.0.1), Ipv4Addr::UNSPECIFIED (0.0.0.0) and Ipv6Addr::LOCALHOST (::1) can be used in statics.
  • Binding a server to 0.0.0.0 listens on every IPv4 interface; binding to 127.0.0.1 accepts local connections only.
  • Ipv4Addr converts to and from u32 (big-endian: 1.2.3.4 is 0x01020304) and from [u8; 4], which is handy for subnet math.
  • Ordering compares the numeric value, so a Vec<Ipv4Addr> sorts as you would expect from the dotted form.
  • Display of IPv6 follows the compressed form (2001:db8::1); the textual form does not preserve how you originally wrote it.
  • Parsing rejects leading zeros in IPv4 octets ("010.0.0.1" is an error) to avoid octal confusion.
  • For classification the standard library does not cover (for example a specific subnet), compare the octets or the u32 value with a mask.
  • ToSocketAddrs accepts strings like "localhost:80" and may perform a DNS lookup that blocks; do not call it in latency-sensitive code without care.