ip
import std::net::address::ip; · source
Types
IpV4Address
type struct IpV4Address {
a: u8;
b: u8;
c: u8;
d: u8;
}
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 (failing with a ConversionError), parse_opt as an Option. to_u32 gives the address in host byte order, which is what the raw socket API wants after htonl. Display prints it in dotted decimal.
Properties
| Property | Type |
|---|---|
a | u8 |
b | u8 |
c | u8 |
d | u8 |
Constructors
static new(a: u8, b: u8, c: u8, d: u8) -> IpV4Address;
static new(a: u8, b: u8, c: u8, d: u8) -> IpV4Address;The address a.b.c.d.
static unspecified() -> IpV4Address;
static unspecified() -> IpV4Address;0.0.0.0: "any interface" for a bound listener.
static localhost() -> IpV4Address;
static localhost() -> IpV4Address;127.0.0.1.
static broadcast() -> IpV4Address;
static broadcast() -> IpV4Address;255.255.255.255.
static parse(source: Str) -> Result<IpV4Address, ConversionError>;
static parse(source: Str) -> Result<IpV4Address, 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<IpV4Address>;
static parse_opt(source: Str) -> Option<IpV4Address>;Detail-free variant of parse: None on any failure. Backs parse and suits callers that branch only on success/failure.
Methods
octet(&this, index: i64) -> u8;
octet(&this, index: i64) -> u8;The octet at index, counted in network order: 0 is the most significant (a in a.b.c.d), 3 the least. Panics when index is outside 0..4.
is_loopback(&this) -> boolean;
is_loopback(&this) -> boolean;Whether the address is in the loopback block 127.0.0.0/8 - any address whose first octet is 127, not only 127.0.0.1.
is_unspecified(&this) -> boolean;
is_unspecified(&this) -> boolean;Whether the address is 0.0.0.0.
to_u32(&this) -> u32;
to_u32(&this) -> u32;Host-byte-order u32 representation. High byte is a.
IpV6Address
type struct IpV6Address {
segments: [u8; 16];
}
The same shape over sixteen bytes, with group_at reading one 16-bit group and from_bytes taking the sixteen bytes from a slice (None if it is not exactly sixteen long). 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() -> IpV6Address;
static unspecified() -> IpV6Address;::, all sixteen octets zero: "any interface" for a bound listener.
static localhost() -> IpV6Address;
static localhost() -> IpV6Address;::1, the IPv6 loopback address.
static from_bytes(bytes: &Slice<u8>) -> Option<IpV6Address>;
static from_bytes(bytes: &Slice<u8>) -> Option<IpV6Address>;The address held in sixteen network-order octets (e.g. the sin6_addr of a kernel-filled sockaddr_in6), or None unless bytes is exactly sixteen long.
static parse(source: Str) -> Result<IpV6Address, ConversionError>;
static parse(source: Str) -> Result<IpV6Address, 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<IpV6Address>;
static parse_opt(source: Str) -> Option<IpV6Address>;Detail-free variant of parse: None on any malformed input.
Methods
group_at(&this, group: i64) -> u16;
group_at(&this, group: i64) -> 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;::.
IpAddress
type enum IpAddress {
V4(IpV4Address);
V6(IpV6Address);
}
The enum over the two, with the same parse pair: text containing a : goes to the v6 parser, anything else to v4. Display writes the family's own form.
import std::collections::str::{ Str };
import std::net::address::ip::{ IpAddress, IpV4Address };
const local: IpAddress = IpAddress::V4(IpV4Address::localhost());
match (IpAddress::parse(Str::new("2001:db8::1"))) {
Result::Ok(address) => { /* address.is_ipv6() */ }
Result::Err(error) => { /* not an address */ }
}
All three are POD — copied by value, nothing to drop.
Variants
| Variant |
|---|
V4(IpV4Address) |
V6(IpV6Address) |
Constructors
static parse(source: Str) -> Result<IpAddress, ConversionError>;
static parse(source: Str) -> Result<IpAddress, 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<IpAddress>;
static parse_opt(source: Str) -> Option<IpAddress>;Detail-free variant of parse: None if the selected parser rejects.
Methods
is_ipv4(&this) -> boolean;
is_ipv4(&this) -> boolean;Whether this is an IPv4 address (IpAddress::V4).
is_ipv6(&this) -> boolean;
is_ipv6(&this) -> boolean;Whether this is an IPv6 address (IpAddress::V6).
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 IpV4Address {
a: u8;
b: u8;
c: u8;
d: u8;
}An IPv4 address as its four octets, a.b.c.d, a the most significant. Printed in dotted decimal by Display.
IpV4Address.a: u8IpV4Address.b: u8IpV4Address.c: u8IpV4Address.d: u8type struct IpV6Address {
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.
IpV6Address.segments: [u8; 16]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 IpAddress {
V4(IpV4Address);
V6(IpV6Address);
}An IP address of either family. parse picks the family from the text (a : means IPv6), and Display writes the family's own form.
IpAddress::V4(IpV4Address)type struct IpV4Address {
a: u8;
b: u8;
c: u8;
d: u8;
}An IPv4 address as its four octets, a.b.c.d, a the most significant. Printed in dotted decimal by Display.
IpAddress::V6(IpV6Address)type struct IpV6Address {
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.
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).
type struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
type enum IpAddress {
V4(IpV4Address);
V6(IpV6Address);
}An IP address of either family. parse picks the family from the text (a : means IPv6), and Display writes the family's own form.
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: IpAddressIpAddress::V4(IpV4Address)match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, constants, and tuple and destructuring patterns, such as (0, 1..8) or Option::Some((count, _)), 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 IpAddress::parse(source: Str) -> Result<IpAddress, 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.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
Result::Ok(T)address: IpAddresspattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)error: ConversionErrorstatic IpV4Address::new(a: u8, b: u8, c: u8, d: u8) -> IpV4AddressThe address a.b.c.d.
IpV4Address.a: u8IpV4Address.b: u8IpV4Address.c: u8IpV4Address.d: u8static IpV4Address::unspecified() -> IpV4Address0.0.0.0: "any interface" for a bound listener.
static IpV4Address::parse(source: Str) -> Result<IpV4Address, 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.
type struct ConversionError {}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
static IpV4Address::parse_opt(source: Str) -> Option<IpV4Address>Detail-free variant of parse: None on any failure. Backs parse and suits callers that branch only on success/failure.
type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
IpV4Address::to_u32(&this) -> u32Host-byte-order u32 representation. High byte is a.
type trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
static 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.
(parameter) a: u8(parameter) b: u8(parameter) c: u8(parameter) d: u8(parameter) source: StrOption::NoneIpV4Address::octet(&this, index: i64) -> u8The octet at index, counted in network order: 0 is the most significant (a in a.b.c.d), 3 the least. Panics when index is outside 0..4.
&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: i64IpV4Address::is_loopback(&this) -> booleanWhether the address is in the loopback block 127.0.0.0/8 - any address whose first octet is 127, not only 127.0.0.1.
IpV6Address::group_at(&this, group: i64) -> u16The two-byte group at group (0..8), big-endian (segments are in network order, so the high byte comes first).
static IpV6Address::from_bytes(bytes: &Slice<u8>) -> Option<IpV6Address>The address held in sixteen network-order octets (e.g. the sin6_addr of a kernel-filled sockaddr_in6), or None unless bytes is exactly sixteen long.
IpV6Address.segments: [u8; 16](parameter) bytes: &Slice<u8>type struct Slice<T> {}The elements are reached through ptr, declared read-only: through a mut &Slice<T> it reads as a writable pointer, through a &Slice<T> as the read-only one it is.
(parameter) group: i64IpAddress::V6(IpV6Address)IpAddress::is_ipv4(&this) -> booleanWhether this is an IPv4 address (IpAddress::V4).
IpAddress::is_ipv6(&this) -> booleanWhether this is an IPv6 address (IpAddress::V6).