Struct Ipv6Addr
struct Ipv6Addr { ... }
An IPv6 address.
IPv6 addresses are defined as 128-bit integers in IETF RFC 4291. They are usually represented as eight 16-bit segments.
Embedding IPv4 Addresses
See IpAddr for a type encompassing both IPv4 and IPv6 addresses.
To assist in the transition from IPv4 to IPv6 two types of IPv6 addresses that embed an IPv4 address were defined: IPv4-compatible and IPv4-mapped addresses. Of these IPv4-compatible addresses have been officially deprecated.
Both types of addresses are not assigned any special meaning by this implementation,
other than what the relevant standards prescribe. This means that an address like ::ffff:127.0.0.1,
while representing an IPv4 loopback address, is not itself an IPv6 loopback address; only ::1 is.
To handle these so called "IPv4-in-IPv6" addresses, they have to first be converted to their canonical IPv4 address.
IPv4-Compatible IPv6 Addresses
IPv4-compatible IPv6 addresses are defined in IETF RFC 4291 Section 2.5.5.1, and have been officially deprecated. The RFC describes the format of an "IPv4-Compatible IPv6 address" as follows:
| 80 bits | 16 | 32 bits |
+--------------------------------------+--------------------------+
|0000..............................0000|0000| IPv4 address |
+--------------------------------------+----+---------------------+
So ::a.b.c.d would be an IPv4-compatible IPv6 address representing the IPv4 address a.b.c.d.
To convert from an IPv4 address to an IPv4-compatible IPv6 address, use Ipv4Addr::to_ipv6_compatible.
Use Ipv6Addr::to_ipv4 to convert an IPv4-compatible IPv6 address to the canonical IPv4 address.
IPv4-Mapped IPv6 Addresses
IPv4-mapped IPv6 addresses are defined in IETF RFC 4291 Section 2.5.5.2. The RFC describes the format of an "IPv4-Mapped IPv6 address" as follows:
| 80 bits | 16 | 32 bits |
+--------------------------------------+--------------------------+
|0000..............................0000|FFFF| IPv4 address |
+--------------------------------------+----+---------------------+
So ::ffff:a.b.c.d would be an IPv4-mapped IPv6 address representing the IPv4 address a.b.c.d.
To convert from an IPv4 address to an IPv4-mapped IPv6 address, use Ipv4Addr::to_ipv6_mapped.
Use Ipv6Addr::to_ipv4 to convert an IPv4-mapped IPv6 address to the canonical IPv4 address.
Note that this will also convert the IPv6 loopback address ::1 to 0.0.0.1. Use
Ipv6Addr::to_ipv4_mapped to avoid this.
Textual representation
Ipv6Addr provides a FromStr implementation. There are many ways to represent
an IPv6 address in text, but in general, each segments is written in hexadecimal
notation, and segments are separated by :. For more information, see
IETF RFC 5952.
Examples
use Ipv6Addr;
let localhost = new;
assert_eq!;
assert_eq!;
Implementations
impl Ipv6Addr
const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6AddrCreates a new IPv6 address from eight 16-bit segments.
The result will represent the IP address
a:b:c:d:e:f:g:h.Examples
use Ipv6Addr; let addr = new;const fn to_bits(self: Self) -> u128Converts an IPv6 address into a
u128representation using native byte order.Although IPv6 addresses are big-endian, the
u128value will use the target platform's native byte order. That is, theu128value is an integer representation of the IPv6 address and not an integer interpretation of the IPv6 address's big-endian bitstring. This means that theu128value masked with0xffffffffffffffffffffffffffff0000_u128will set the last segment in the address to 0, regardless of the target platform's endianness.Examples
use Ipv6Addr; let addr = new; assert_eq!;use Ipv6Addr; let addr = new; let addr_bits = addr.to_bits & 0xffffffffffffffffffffffffffff0000_u128; assert_eq!;const fn from_bits(bits: u128) -> Ipv6AddrConverts a native byte order
u128into an IPv6 address.See
Ipv6Addr::to_bitsfor an explanation on endianness.Examples
use Ipv6Addr; let addr = from_bits; assert_eq!;const fn segments(self: &Self) -> [u16; 8]Returns the eight 16-bit segments that make up this address.
Examples
use Ipv6Addr; assert_eq!;const fn from_segments(segments: [u16; 8]) -> Ipv6AddrCreates an
Ipv6Addrfrom an eight element 16-bit array.Examples
use Ipv6Addr; let addr = from_segments; assert_eq!;const fn is_unspecified(self: &Self) -> boolReturns
truefor the special 'unspecified' address (::).This property is defined in IETF RFC 4291.
Examples
use Ipv6Addr; assert_eq!; assert_eq!;const fn is_loopback(self: &Self) -> boolReturns
trueif this is the loopback address (::1), as defined in IETF RFC 4291 section 2.5.3.Contrary to IPv4, in IPv6 there is only one loopback address.
Examples
use Ipv6Addr; assert_eq!; assert_eq!;const fn is_global(self: &Self) -> boolReturns
trueif the address appears to be globally reachable as specified by the IANA IPv6 Special-Purpose Address Registry.Whether or not an address is practically reachable will depend on your network configuration. Most IPv6 addresses are globally reachable, unless they are specifically defined as not globally reachable.
Non-exhaustive list of notable addresses that are not globally reachable:
- The unspecified address (
is_unspecified) - The loopback address (
is_loopback) - IPv4-mapped addresses
- Addresses reserved for benchmarking (
is_benchmarking) - Addresses reserved for documentation (
is_documentation) - Unique local addresses (
is_unique_local) - Unicast addresses with link-local scope (
is_unicast_link_local)
For the complete overview of which addresses are globally reachable, see the table at the IANA IPv6 Special-Purpose Address Registry.
Note that an address having global scope is not the same as being globally reachable, and there is no direct relation between the two concepts: There exist addresses with global scope that are not globally reachable (for example unique local addresses), and addresses that are globally reachable without having global scope (multicast addresses with non-global scope).
Examples
use Ipv6Addr; // Most IPv6 addresses are globally reachable: assert_eq!; // However some addresses have been assigned a special meaning // that makes them not globally reachable. Some examples are: // The unspecified address (`::`) assert_eq!; // The loopback address (`::1`) assert_eq!; // IPv4-mapped addresses (`::ffff:0:0/96`) assert_eq!; // Addresses reserved for benchmarking (`2001:2::/48`) assert_eq!; // Addresses reserved for documentation (`2001:db8::/32` and `3fff::/20`) assert_eq!; assert_eq!; // Unique local addresses (`fc00::/7`) assert_eq!; // Unicast addresses with link-local scope (`fe80::/10`) assert_eq!; // For a complete overview see the IANA IPv6 Special-Purpose Address Registry.- The unspecified address (
const fn is_unique_local(self: &Self) -> boolReturns
trueif this is a unique local address (fc00::/7).This property is defined in IETF RFC 4193.
Examples
use Ipv6Addr; assert_eq!; assert_eq!;const fn is_unicast(self: &Self) -> boolReturns
trueif this is a unicast address, as defined by IETF RFC 4291. Any address that is not a multicast address (ff00::/8) is unicast.Examples
use Ipv6Addr; // The unspecified and loopback addresses are unicast. assert_eq!; assert_eq!; // Any address that is not a multicast address (`ff00::/8`) is unicast. assert_eq!; assert_eq!;const fn is_unicast_link_local(self: &Self) -> boolReturns
trueif the address is a unicast address with link-local scope, as defined in RFC 4291.A unicast address has link-local scope if it has the prefix
fe80::/10, as per RFC 4291 section 2.4. Note that this encompasses more addresses than those defined in RFC 4291 section 2.5.6, which describes "Link-Local IPv6 Unicast Addresses" as having the following stricter format:| 10 bits | 54 bits | 64 bits | +----------+-------------------------+----------------------------+ |1111111010| 0 | interface ID | +----------+-------------------------+----------------------------+So while currently the only addresses with link-local scope an application will encounter are all in
fe80::/64, this might change in the future with the publication of new standards. More addresses infe80::/10could be allocated, and those addresses will have link-local scope.Also note that while RFC 4291 section 2.5.3 mentions about the loopback address (
::1) that "it is treated as having Link-Local scope", this does not mean that the loopback address actually has link-local scope and this method will returnfalseon it.Examples
use Ipv6Addr; // The loopback address (`::1`) does not actually have link-local scope. assert_eq!; // Only addresses in `fe80::/10` have link-local scope. assert_eq!; assert_eq!; // Addresses outside the stricter `fe80::/64` also have link-local scope. assert_eq!; assert_eq!;const fn is_documentation(self: &Self) -> boolReturns
trueif this is an address reserved for documentation (2001:db8::/32and3fff::/20).This property is defined by IETF RFC 3849 and IETF RFC 9637.
Examples
use Ipv6Addr; assert_eq!; assert_eq!; assert_eq!;const fn is_benchmarking(self: &Self) -> boolReturns
trueif this is an address reserved for benchmarking (2001:2::/48).This property is defined in IETF RFC 5180, where it is mistakenly specified as covering the range
2001:0200::/48. This is corrected in IETF RFC Errata 1752 to2001:0002::/48.use Ipv6Addr; assert_eq!; assert_eq!;const fn is_unicast_global(self: &Self) -> boolReturns
trueif the address is a globally routable unicast address.The following return false:
- the loopback address
- the link-local addresses
- unique local addresses
- the unspecified address
- the address range reserved for documentation
This method returns
truefor site-local addresses as per RFC 4291 section 2.5.7The special behavior of [the site-local unicast] prefix defined in [RFC3513] must no longer be supported in new implementations (i.e., new implementations must treat this prefix as Global Unicast).Examples
use Ipv6Addr; assert_eq!; assert_eq!;const fn multicast_scope(self: &Self) -> Option<Ipv6MulticastScope>Returns the address's multicast scope if the address is multicast.
Examples
use ; assert_eq!; assert_eq!;const fn is_multicast(self: &Self) -> boolReturns
trueif this is a multicast address (ff00::/8).This property is defined by IETF RFC 4291.
Examples
use Ipv6Addr; assert_eq!; assert_eq!;const fn is_ipv4_mapped(self: &Self) -> boolReturns
trueif the address is an IPv4-mapped address (::ffff:0:0/96).IPv4-mapped addresses can be converted to their canonical IPv4 address with
to_ipv4_mapped.Examples
use ; let ipv4_mapped = new.to_ipv6_mapped; assert_eq!; assert_eq!; assert_eq!;const fn to_ipv4_mapped(self: &Self) -> Option<Ipv4Addr>Converts this address to an
IPv4address if it's an IPv4-mapped address, as defined in IETF RFC 4291 section 2.5.5.2, otherwise returnsNone.::ffff:a.b.c.dbecomesa.b.c.d. All addresses not starting with::ffffwill returnNone.Examples
use ; assert_eq!; assert_eq!; assert_eq!;const fn to_ipv4(self: &Self) -> Option<Ipv4Addr>Converts this address to an
IPv4address if it is either an IPv4-compatible address as defined in IETF RFC 4291 section 2.5.5.1, or an IPv4-mapped address as defined in IETF RFC 4291 section 2.5.5.2, otherwise returnsNone.Note that this will return an
IPv4address for the IPv6 loopback address::1. UseIpv6Addr::to_ipv4_mappedto avoid this.::a.b.c.dand::ffff:a.b.c.dbecomea.b.c.d.::1becomes0.0.0.1. All addresses not starting with either all zeroes or::ffffwill returnNone.Examples
use ; assert_eq!; assert_eq!; assert_eq!;const fn to_canonical(self: &Self) -> IpAddrConverts this address to an
IpAddr::V4if it is an IPv4-mapped address, otherwise returns self wrapped in anIpAddr::V6.Examples
use Ipv6Addr; assert_eq!; assert_eq!;const fn octets(self: &Self) -> [u8; 16]Returns the sixteen eight-bit integers the IPv6 address consists of.
use Ipv6Addr; assert_eq!;const fn from_octets(octets: [u8; 16]) -> Ipv6AddrCreates an
Ipv6Addrfrom a sixteen element byte array.Examples
use Ipv6Addr; let addr = from_octets; assert_eq!;const fn as_octets(self: &Self) -> &[u8; 16]Returns the sixteen eight-bit integers the IPv6 address consists of as a slice.
Examples
use Ipv6Addr; assert_eq!
impl Ipv6Addr
fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError>Parse an IPv6 address from a slice of bytes.
use Ipv6Addr; let localhost = new; assert_eq!;
impl BitAnd for Ipv6Addr
fn bitand(self: Self, rhs: Ipv6Addr) -> Ipv6Addr
impl BitAnd for Ipv6Addr
fn bitand(self: Self, rhs: &Ipv6Addr) -> Ipv6Addr
impl BitAndAssign for Ipv6Addr
fn bitand_assign(self: &mut Self, rhs: Ipv6Addr)
impl BitAndAssign for Ipv6Addr
fn bitand_assign(self: &mut Self, rhs: &Ipv6Addr)
impl BitOr for Ipv6Addr
fn bitor(self: Self, rhs: Ipv6Addr) -> Ipv6Addr
impl BitOr for Ipv6Addr
fn bitor(self: Self, rhs: &Ipv6Addr) -> Ipv6Addr
impl BitOrAssign for Ipv6Addr
fn bitor_assign(self: &mut Self, rhs: Ipv6Addr)
impl BitOrAssign for Ipv6Addr
fn bitor_assign(self: &mut Self, rhs: &Ipv6Addr)
impl Clone for Ipv6Addr
fn clone(self: &Self) -> Ipv6Addr
impl Copy for Ipv6Addr
impl Debug for Ipv6Addr
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl Display for Ipv6Addr
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Eq for Ipv6Addr
impl Freeze for Ipv6Addr
impl From for Ipv6Addr
fn from(ip: u128) -> Ipv6AddrUses
Ipv6Addr::from_bitsto convert a host byte orderu128to an IPv6 address.
impl From for Ipv6Addr
fn from(octets: [u8; 16]) -> Ipv6AddrCreates an
Ipv6Addrfrom a sixteen element byte array.Examples
use Ipv6Addr; let addr = from; assert_eq!;
impl From for Ipv6Addr
fn from(segments: [u16; 8]) -> Ipv6AddrCreates an
Ipv6Addrfrom an eight element 16-bit array.Examples
use Ipv6Addr; let addr = from; assert_eq!;
impl FromStr for Ipv6Addr
fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError>
impl Hash for Ipv6Addr
fn hash<H: Hasher>(self: &Self, state: &mut H)
impl Not for Ipv6Addr
fn not(self: Self) -> Ipv6Addr
impl Ord for Ipv6Addr
fn cmp(self: &Self, other: &Ipv6Addr) -> Ordering
impl PartialEq for Ipv6Addr
fn eq(self: &Self, other: &IpAddr) -> bool
impl PartialEq for Ipv6Addr
fn eq(self: &Self, other: &Ipv6Addr) -> bool
impl PartialOrd for Ipv6Addr
fn partial_cmp(self: &Self, other: &Ipv6Addr) -> Option<Ordering>
impl PartialOrd for Ipv6Addr
fn partial_cmp(self: &Self, other: &IpAddr) -> Option<Ordering>
impl RefUnwindSafe for Ipv6Addr
impl Send for Ipv6Addr
impl Step for Ipv6Addr
fn steps_between(start: &Ipv6Addr, end: &Ipv6Addr) -> (usize, Option<usize>)fn forward_checked(start: Ipv6Addr, count: usize) -> Option<Ipv6Addr>fn backward_checked(start: Ipv6Addr, count: usize) -> Option<Ipv6Addr>unsafe fn forward_unchecked(start: Ipv6Addr, count: usize) -> Ipv6Addrunsafe fn backward_unchecked(start: Ipv6Addr, count: usize) -> Ipv6Addr
impl StructuralPartialEq for Ipv6Addr
impl Sync for Ipv6Addr
impl TrustedStep for Ipv6Addr
impl Unpin for Ipv6Addr
impl UnsafeUnpin for Ipv6Addr
impl UnwindSafe for Ipv6Addr
impl<T> Any for Ipv6Addr
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Ipv6Addr
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Ipv6Addr
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Ipv6Addr
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Ipv6Addr
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for Ipv6Addr
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Ipv6Addr
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Ipv6Addr
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>