ip
import std::net::addr::ip; · source
Types
IpV4Addr
type struct IpV4Addr {
a: u8;
b: u8;
c: u8;
d: u8;
static new(a: u8, b: u8, c: u8, d: u8) -> IpV4Addr;
static unspecified() -> IpV4Addr;
static localhost() -> IpV4Addr;
static broadcast() -> IpV4Addr;
octet(&this, index: u64) -> u8;
is_loopback(&this) -> boolean;
is_unspecified(&this) -> boolean;
to_u32(&this) -> u32;
static parse(source: Str) -> Result<IpV4Addr, ConversionError>;
static parse_opt(source: Str) -> Option<IpV4Addr>;
}
new(a, b, c, d) builds an address from its octets; localhost(), unspecified(), and broadcast() are the named ones. parse reads dotted-quad text as a Result, parse_opt as an Option. to_u32 gives the address in host byte order, which is what the raw socket API wants after htonl.
Properties
| Property | Type |
|---|---|
a | u8 |
b | u8 |
c | u8 |
d | u8 |
Constructors
static new(a: u8, b: u8, c: u8, d: u8) -> IpV4Addr;
static unspecified() -> IpV4Addr;
static unspecified() -> IpV4Addr;0.0.0.0: "any interface" for a bound listener.
static localhost() -> IpV4Addr;
static localhost() -> IpV4Addr;127.0.0.1.
static broadcast() -> IpV4Addr;
static broadcast() -> IpV4Addr;255.255.255.255.
static parse(source: Str) -> Result<IpV4Addr, ConversionError>;
static parse(source: Str) -> Result<IpV4Addr, ConversionError>;Parse canonical a.b.c.d. Returns Err(ConversionError) on any failure (extra whitespace, out-of-range octets, missing or extra dots, leading/trailing separators); the error is describable/printable. Use parse_opt if you only need presence, not the reason.
static parse_opt(source: Str) -> Option<IpV4Addr>;
static parse_opt(source: Str) -> Option<IpV4Addr>;Detail-free variant of parse: None on any failure. Backs parse and suits callers that branch only on success/failure.
Methods
octet(&this, index: u64) -> u8;
octet(&this, index: u64) -> u8;Returns the four octets in big-endian (network) order.
is_loopback(&this) -> boolean;
is_unspecified(&this) -> boolean;
to_u32(&this) -> u32;
to_u32(&this) -> u32;Host-byte-order u32 representation. High byte is a.
IpV6Addr
type struct IpV6Addr {
segments: [u8;
16];
static unspecified() -> IpV6Addr;
static localhost() -> IpV6Addr;
static from_buf(p: u8*) -> IpV6Addr;
group_at(&this, group: u64) -> u16;
is_loopback(&this) -> boolean;
is_unspecified(&this) -> boolean;
static parse(source: Str) -> Result<IpV6Addr, ConversionError>;
static parse_opt(source: Str) -> Option<IpV6Addr>;
}
The same shape over sixteen bytes, with group_at reading one 16-bit group. parse reads the canonical text forms: full a:b:c:d:e:f:g:h, one ::-compressed run, and a trailing embedded IPv4 (::ffff:1.2.3.4).
Properties
| Property | Type |
|---|---|
segments | [u8 |
16] |
Constructors
static unspecified() -> IpV6Addr;
static localhost() -> IpV6Addr;
static from_buf(p: u8*) -> IpV6Addr;
static from_buf(p: u8*) -> IpV6Addr;Build from sixteen network-order octets at p[0..16] (e.g. the sin6_addr of a kernel-filled sockaddr_in6).
static parse(source: Str) -> Result<IpV6Addr, ConversionError>;
static parse(source: Str) -> Result<IpV6Addr, ConversionError>;Parse canonical IPv6 text: full a:b:c:d:e:f:g:h, ::-compressed (one run at most), and a trailing embedded IPv4 (::ffff:1.2.3.4). Returns Err(ConversionError) on any malformed input; use parse_opt for the detail-free variant.
static parse_opt(source: Str) -> Option<IpV6Addr>;
static parse_opt(source: Str) -> Option<IpV6Addr>;Detail-free variant of parse: None on any malformed input.
Methods
group_at(&this, group: u64) -> u16;
group_at(&this, group: u64) -> u16;The two-byte group at group (0..8), big-endian (segments are in network order, so the high byte comes first).
is_loopback(&this) -> boolean;
is_loopback(&this) -> boolean;::1.
is_unspecified(&this) -> boolean;
is_unspecified(&this) -> boolean;::.
IpAddr
type enum IpAddr {
V4(IpV4Addr);
V6(IpV6Addr);
}
implement enum IpAddr {
is_ipv4(&this) -> boolean;
is_ipv6(&this) -> boolean;
static parse(source: Str) -> Result<IpAddr, ConversionError>;
static parse_opt(source: Str) -> Option<IpAddr>;
}
The enum over the two, with the same parse pair: text containing a : goes to the v6 parser, anything else to v4.
import std::net::addr::ip;
const local: IpAddr = IpAddr::V4(IpV4Addr::localhost());
match (IpAddr::parse(Str::new("2001:db8::1"))) {
Result::Ok(addr) => { /* addr.is_ipv6() */ }
Result::Err(e) => { /* not an address */ }
}
All three are POD — copied by value, nothing to drop.
Variants
| Variant |
|---|
V4(IpV4Addr) |
V6(IpV6Addr) |
Constructors
static parse(source: Str) -> Result<IpAddr, ConversionError>;
static parse(source: Str) -> Result<IpAddr, ConversionError>;Parse textual form. A ':' routes to the v6 parser; otherwise v4. Returns Err(ConversionError) if the selected parser rejects the input; use parse_opt for the detail-free variant.
static parse_opt(source: Str) -> Option<IpAddr>;
static parse_opt(source: Str) -> Option<IpAddr>;Detail-free variant of parse: None if the selected parser rejects.
Methods
is_ipv4(&this) -> boolean;
is_ipv6(&this) -> boolean;
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct IpV4Addr {
a: u8;
b: u8;
c: u8;
d: u8;
}IpV4Addr.a: u8IpV4Addr.b: u8IpV4Addr.c: u8IpV4Addr.d: u8static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static IpV4Addr::new(a: u8, b: u8, c: u8, d: u8) -> IpV4Addr(parameter) a: u8(parameter) b: u8(parameter) c: u8(parameter) d: u8type struct IpV4Addr {
a: u8;
b: u8;
c: u8;
d: u8;
}static IpV4Addr::unspecified() -> IpV4Addr0.0.0.0: "any interface" for a bound listener.
static IpV4Addr::localhost() -> IpV4Addr127.0.0.1.
static IpV4Addr::broadcast() -> IpV4Addr255.255.255.255.
IpV4Addr::octet(&this, index: u64) -> u8Returns the four octets in big-endian (network) order.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) index: u64IpV4Addr::is_loopback(&this) -> booleanIpV4Addr::is_unspecified(&this) -> booleanIpV4Addr::to_u32(&this) -> u32Host-byte-order u32 representation. High byte is a.
static IpV4Addr::parse(source: Str) -> Result<IpV4Addr, ConversionError>Parse canonical a.b.c.d. Returns Err(ConversionError) on any failure (extra whitespace, out-of-range octets, missing or extra dots, leading/trailing separators); the error is describable/printable. Use parse_opt if you only need presence, not the reason.
(parameter) source: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype struct ConversionError {
reason: string;
}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
static IpV4Addr::parse_opt(source: Str) -> Option<IpV4Addr>Detail-free variant of parse: None on any failure. Backs parse and suits callers that branch only on success/failure.
type struct IpV6Addr {
segments: [u8;
16];
}Sixteen octets in network byte order. parse accepts the canonical text forms (full 8-group, ::-compressed, and trailing embedded-v4); addresses may also be constructed directly.
IpV6Addr.segments: [u8static IpV6Addr::unspecified() -> IpV6Addrtype struct IpV6Addr {
segments: [u8;
16];
}Sixteen octets in network byte order. parse accepts the canonical text forms (full 8-group, ::-compressed, and trailing embedded-v4); addresses may also be constructed directly.
static IpV6Addr::localhost() -> IpV6Addrstatic IpV6Addr::from_buf(p: u8*) -> IpV6AddrBuild from sixteen network-order octets at p[0..16] (e.g. the sin6_addr of a kernel-filled sockaddr_in6).
(parameter) p: u8*IpV6Addr::group_at(&this, group: u64) -> u16The two-byte group at group (0..8), big-endian (segments are in network order, so the high byte comes first).
(parameter) group: u64IpV6Addr::is_loopback(&this) -> boolean::1.
IpV6Addr::is_unspecified(&this) -> boolean::.
static IpV6Addr::parse(source: Str) -> Result<IpV6Addr, ConversionError>Parse canonical IPv6 text: full a:b:c:d:e:f:g:h, ::-compressed (one run at most), and a trailing embedded IPv4 (::ffff:1.2.3.4). Returns Err(ConversionError) on any malformed input; use parse_opt for the detail-free variant.
static IpV6Addr::parse_opt(source: Str) -> Option<IpV6Addr>Detail-free variant of parse: None on any malformed input.
type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum IpAddr {
V4(IpV4Addr);
V6(IpV6Addr);
}IpAddr::V4(IpV4Addr)IpAddr::V6(IpV6Addr)implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement enum Name { ... }Names the enum an inherent block extends - the only way to give an enum methods.
language referencetype enum IpAddr {
V4(IpV4Addr);
V6(IpV6Addr);
}IpAddr::is_ipv4(&this) -> booleanIpAddr::is_ipv6(&this) -> booleanstatic IpAddr::parse(source: Str) -> Result<IpAddr, ConversionError>Parse textual form. A ':' routes to the v6 parser; otherwise v4. Returns Err(ConversionError) if the selected parser rejects the input; use parse_opt for the detail-free variant.
static IpAddr::parse_opt(source: Str) -> Option<IpAddr>Detail-free variant of parse: None if the selected parser rejects.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const local: IpAddrIpAddr::V4(IpV4Addr)match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
static IpAddr::parse(source: Str) -> Result<IpAddr, ConversionError>Parse textual form. A ':' routes to the v6 parser; otherwise v4. Returns Err(ConversionError) if the selected parser rejects the input; use parse_opt for the detail-free variant.
static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
Result::Ok(T)addr: IpAddrpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)e: ConversionErrorstatic IpV4Addr::new(a: u8, b: u8, c: u8, d: u8) -> IpV4AddrIpV4Addr.a: u8IpV4Addr.b: u8IpV4Addr.c: u8IpV4Addr.d: u8static IpV4Addr::unspecified() -> IpV4Addr0.0.0.0: "any interface" for a bound listener.
static IpV4Addr::parse(source: Str) -> Result<IpV4Addr, ConversionError>Parse canonical a.b.c.d. Returns Err(ConversionError) on any failure (extra whitespace, out-of-range octets, missing or extra dots, leading/trailing separators); the error is describable/printable. Use parse_opt if you only need presence, not the reason.
static IpV4Addr::parse_opt(source: Str) -> Option<IpV4Addr>Detail-free variant of parse: None on any failure. Backs parse and suits callers that branch only on success/failure.
Option::NoneIpV4Addr::octet(&this, index: u64) -> u8Returns the four octets in big-endian (network) order.
std::net::addr::ipIpV4Addr::is_loopback(&this) -> booleanIpV4Addr::is_unspecified(&this) -> booleanIpV6Addr::group_at(&this, group: u64) -> u16The two-byte group at group (0..8), big-endian (segments are in network order, so the high byte comes first).
IpV6Addr.segments: [u8static IpV6Addr::from_buf(p: u8*) -> IpV6AddrBuild from sixteen network-order octets at p[0..16] (e.g. the sin6_addr of a kernel-filled sockaddr_in6).
start..endA half-open range - start up to but excluding end - and sugar for Range::new(start, end): an iterator usable in for, and an ordinary value anywhere else. It binds looser than arithmetic, so a..b + 1 is a..(b + 1).
IpAddr::V6(IpV6Addr)IpAddr::is_ipv4(&this) -> booleanIpAddr::is_ipv6(&this) -> boolean