std::net::Ipv6Addr - Rust
Description: API documentation for the Rust `Ipv6Addr` struct in crate `std`.
Keywords: rust, rustlang, rust-lang, Ipv6Addr

# 1.0.0[−][src]Struct std::net::Ipv6Addr

    pub struct Ipv6Addr { /* fields omitted */ }

An IPv6 address.

IPv6 addresses are defined as 128-bit integers in IETF RFC 4291. They are usually represented as eight 16-bit segments.

See `IpAddr` for a type encompassing both IPv4 and IPv6 addresses.

The size of an `Ipv6Addr` struct may vary depending on the target operating system.

# 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 std::net::Ipv6Addr;

    let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
    assert_eq!("::1".parse(), Ok(localhost));
    assert_eq!(localhost.is_loopback(), true);Run

## Methods

### `impl Ipv6Addr`[src]

#### `pub const fn new(
a: u16,
b: u16,
c: u16,
d: u16,
e: u16,
f: u16,
g: u16,
h: u16
) -> Ipv6Addr`[src]

Creates 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 std::net::Ipv6Addr;

    let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);Run

#### `pub const LOCALHOST: Self`1.30.0[src]

An IPv6 address representing localhost: `::1`.

# Examples

    use std::net::Ipv6Addr;

    let addr = Ipv6Addr::LOCALHOST;
    assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));Run

#### `pub const UNSPECIFIED: Self`1.30.0[src]

An IPv6 address representing the unspecified address: `::`

# Examples

    use std::net::Ipv6Addr;

    let addr = Ipv6Addr::UNSPECIFIED;
    assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));Run

#### `pub fn segments(&self) -> [u16; 8]`[src]

Returns the eight 16-bit segments that make up this address.

# Examples

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
               [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);Run

#### `pub fn is_unspecified(&self) -> bool`1.7.0[src]

Returns `true` for the special 'unspecified' address (::).

This property is defined in IETF RFC 4291.

# Examples

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);Run

#### `pub fn is_loopback(&self) -> bool`1.7.0[src]

Returns `true` if this is a loopback address (::1).

This property is defined in IETF RFC 4291.

# Examples

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);Run

#### `pub fn is_global(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns `true` if the address appears to be globally routable.

The following return `false`:

- the loopback address
- link-local, site-local, and unique local unicast addresses
- interface-, link-, realm-, admin- and site-local multicast addresses

# Examples

    #![feature(ip)]

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
    assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);Run

#### `pub fn is_unique_local(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns `true` if this is a unique local address (`fc00::/7`).

This property is defined in IETF RFC 4193.

# Examples

    #![feature(ip)]

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
    assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);Run

#### `pub fn is_unicast_link_local_strict(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns `true` if the address is a unicast link-local address (`fe80::/64`).

A common mis-conception is to think that "unicast link-local addresses start with `fe80::`", but the IETF RFC 4291 actually defines a stricter format for these addresses:

    |   10     |
    |  bits    |         54 bits         |          64 bits           |
    +----------+-------------------------+----------------------------+
    |1111111010|           0             |       interface ID         |
    +----------+-------------------------+----------------------------+

This method validates the format defined in the RFC and won't recognize the following addresses such as `fe80:0:0:1::` or `fe81::` as unicast link-local addresses for example. If you need a less strict validation use `is_unicast_link_local()` instead.

# Examples

    #![feature(ip)]

    use std::net::Ipv6Addr;

    let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0);
    assert!(ip.is_unicast_link_local_strict());

    let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0xffff, 0xffff, 0xffff, 0xffff);
    assert!(ip.is_unicast_link_local_strict());

    let ip = Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0);
    assert!(!ip.is_unicast_link_local_strict());
    assert!(ip.is_unicast_link_local());

    let ip = Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0);
    assert!(!ip.is_unicast_link_local_strict());
    assert!(ip.is_unicast_link_local());Run

# See also

- IETF RFC 4291 section 2.5.6
- RFC 4291 errata 4406
- `is_unicast_link_local()`

#### `pub fn is_unicast_link_local(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns `true` if the address is a unicast link-local address (`fe80::/10`).

This method returns `true` for addresses in the range reserved by [RFC 4291 section 2.4], i.e. addresses with the following format:

    |   10     |
    |  bits    |         54 bits         |          64 bits           |
    +----------+-------------------------+----------------------------+
    |1111111010|    arbitratry value     |       interface ID         |
    +----------+-------------------------+----------------------------+

As a result, this method consider addresses such as `fe80:0:0:1::` or `fe81::` to be unicast link-local addresses, whereas `is_unicast_link_local_strict()` does not. If you need a strict validation fully compliant with the RFC, use `is_unicast_link_local_strict()`.

# Examples

    #![feature(ip)]

    use std::net::Ipv6Addr;

    let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0);
    assert!(ip.is_unicast_link_local());

    let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0xffff, 0xffff, 0xffff, 0xffff);
    assert!(ip.is_unicast_link_local());

    let ip = Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0);
    assert!(ip.is_unicast_link_local());
    assert!(!ip.is_unicast_link_local_strict());

    let ip = Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0);
    assert!(ip.is_unicast_link_local());
    assert!(!ip.is_unicast_link_local_strict());Run

# See also

- IETF RFC 4291 section 2.4
- RFC 4291 errata 4406

#### `pub fn is_unicast_site_local(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns `true` if this is a deprecated unicast site-local address (fec0::/10). The unicast site-local address format is defined in RFC 4291 section 2.5.7 as:

    |   10     |
    |  bits    |         54 bits         |         64 bits            |
    +----------+-------------------------+----------------------------+
    |1111111011|        subnet ID        |       interface ID         |
    +----------+-------------------------+----------------------------+

# Examples

    #![feature(ip)]

    use std::net::Ipv6Addr;

    assert_eq!(
        Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
        false
    );
    assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);Run

# Warning

As per RFC 3879, the whole `FEC0::/10` prefix is deprecated. New software must not support site-local addresses.

#### `pub fn is_documentation(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns `true` if this is an address reserved for documentation (2001:db8::/32).

This property is defined in IETF RFC 3849.

# Examples

    #![feature(ip)]

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
    assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);Run

#### `pub fn is_unicast_global(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns `true` if 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 `true` for site-local addresses as per RFC 4291 section 2.5.7

    The 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

    #![feature(ip)]

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);Run

#### `pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope>`[src]

🔬 This is a nightly-only experimental API. (`ip` #27709)

extra functionality has not been scrutinized to the level that it should be to be stable

Returns the address's multicast scope if the address is multicast.

# Examples

    #![feature(ip)]

    use std::net::{Ipv6Addr, Ipv6MulticastScope};

    assert_eq!(
        Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
        Some(Ipv6MulticastScope::Global)
    );
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);Run

#### `pub fn is_multicast(&self) -> bool`1.7.0[src]

Returns `true` if this is a multicast address (ff00::/8).

This property is defined by IETF RFC 4291.

# Examples

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);Run

#### `pub fn to_ipv4(&self) -> Option<Ipv4Addr>`[src]

Converts this address to an IPv4 address. Returns `None` if this address is neither IPv4-compatible or IPv4-mapped.

::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d

# Examples

    use std::net::{Ipv4Addr, Ipv6Addr};

    assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
               Some(Ipv4Addr::new(192, 10, 2, 255)));
    assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
               Some(Ipv4Addr::new(0, 0, 0, 1)));Run

#### `pub const fn octets(&self) -> [u8; 16]`1.12.0[src]

Returns the sixteen eight-bit integers the IPv6 address consists of.

    use std::net::Ipv6Addr;

    assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
               [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);Run

## Trait Implementations

### `impl From<Ipv6Addr> for IpAddr` 1.16.0[src]

#### `fn from(ipv6: Ipv6Addr) -> IpAddr`[src]

Performs the conversion.

### `impl From<Ipv6Addr> for u128` 1.26.0[src]

#### `fn from(ip: Ipv6Addr) -> u128`[src]

Convert an `Ipv6Addr` into a host byte order `u128`.

# Examples

    use std::net::Ipv6Addr;

    let addr = Ipv6Addr::new(
        0x1020, 0x3040, 0x5060, 0x7080,
        0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
    );
    assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));Run

### `impl From<u128> for Ipv6Addr` 1.26.0[src]

#### `fn from(ip: u128) -> Ipv6Addr`[src]

Convert a host byte order `u128` into an `Ipv6Addr`.

# Examples

    use std::net::Ipv6Addr;

    let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
    assert_eq!(
        Ipv6Addr::new(
            0x1020, 0x3040, 0x5060, 0x7080,
            0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
        ),
        addr);Run

### `impl From<[u8; 16]> for Ipv6Addr` 1.9.0[src]

#### `fn from(octets: [u8; 16]) -> Ipv6Addr`[src]

Performs the conversion.

### `impl From<[u16; 8]> for Ipv6Addr` 1.16.0[src]

#### `fn from(segments: [u16; 8]) -> Ipv6Addr`[src]

Performs the conversion.

### `impl Display for Ipv6Addr`[src]

#### `fn fmt(&self, fmt: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

### `impl Debug for Ipv6Addr`[src]

#### `fn fmt(&self, fmt: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

### `impl PartialEq<Ipv6Addr> for Ipv6Addr`[src]

#### `fn eq(&self, other: &Ipv6Addr) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use] fn ne(&self, other: &Rhs) -> bool`[src]

This method tests for `!=`.

### `impl PartialEq<IpAddr> for Ipv6Addr` 1.16.0[src]

#### `fn eq(&self, other: &IpAddr) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use] fn ne(&self, other: &Rhs) -> bool`[src]

This method tests for `!=`.

### `impl PartialEq<Ipv6Addr> for IpAddr` 1.16.0[src]

#### `fn eq(&self, other: &Ipv6Addr) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use] fn ne(&self, other: &Rhs) -> bool`[src]

This method tests for `!=`.

### `impl Eq for Ipv6Addr`[src]

### `impl Ord for Ipv6Addr`[src]

#### `fn cmp(&self, other: &Ipv6Addr) -> Ordering`[src]

This method returns an `Ordering` between `self` and `other`. Read more

#### `fn max(self, other: Self) -> Self`1.21.0[src]

Compares and returns the maximum of two values. Read more

#### `fn min(self, other: Self) -> Self`1.21.0[src]

Compares and returns the minimum of two values. Read more

#### `fn clamp(self, min: Self, max: Self) -> Self`[src]

🔬 This is a nightly-only experimental API. (`clamp` #44095)

Restrict a value to a certain interval. Read more

### `impl PartialOrd<Ipv6Addr> for Ipv6Addr`[src]

#### `fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering>`[src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `#[must_use] fn lt(&self, other: &Rhs) -> bool`[src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `#[must_use] fn le(&self, other: &Rhs) -> bool`[src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `#[must_use] fn gt(&self, other: &Rhs) -> bool`[src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

#### `#[must_use] fn ge(&self, other: &Rhs) -> bool`[src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

### `impl PartialOrd<Ipv6Addr> for IpAddr` 1.16.0[src]

#### `fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering>`[src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `#[must_use] fn lt(&self, other: &Rhs) -> bool`[src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `#[must_use] fn le(&self, other: &Rhs) -> bool`[src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `#[must_use] fn gt(&self, other: &Rhs) -> bool`[src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

#### `#[must_use] fn ge(&self, other: &Rhs) -> bool`[src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

### `impl PartialOrd<IpAddr> for Ipv6Addr` 1.16.0[src]

#### `fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering>`[src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `#[must_use] fn lt(&self, other: &Rhs) -> bool`[src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `#[must_use] fn le(&self, other: &Rhs) -> bool`[src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `#[must_use] fn gt(&self, other: &Rhs) -> bool`[src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

#### `#[must_use] fn ge(&self, other: &Rhs) -> bool`[src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

### `impl FromStr for Ipv6Addr`[src]

#### `type Err = AddrParseError`

The associated error which can be returned from parsing.

#### `fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError>`[src]

Parses a string `s` to return a value of this type. Read more

### `impl Hash for Ipv6Addr`[src]

#### `fn hash<H: Hasher>(&self, s: &mut H)`[src]

Feeds this value into the given [`Hasher`]. Read more

#### `fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,` 1.3.0[src]

Feeds a slice of this type into the given [`Hasher`]. Read more

### `impl Copy for Ipv6Addr`[src]

### `impl Clone for Ipv6Addr`[src]

#### `fn clone(&self) -> Ipv6Addr`[src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`[src]

Performs copy-assignment from `source`. Read more

## Auto Trait Implementations

### `impl UnwindSafe for Ipv6Addr`

### `impl RefUnwindSafe for Ipv6Addr`

### `impl Unpin for Ipv6Addr`

### `impl Send for Ipv6Addr`

### `impl Sync for Ipv6Addr`

## Blanket Implementations

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, R: Read + ?Sized> Read for &'_ mut Rimpl<'_, W: Write + ?Sized> Write for &'_ mut W`
`fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<'_, R: Read + ?Sized> Read for &'_ mut Rimpl<'_, W: Write + ?Sized> Write for &'_ mut W`
`fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

### `impl<T> ToOwned for T where
T: Clone,` [src]

#### `type Owned = T`

The resulting type after obtaining ownership.

#### `fn to_owned(&self) -> T`[src]

Creates owned data from borrowed data, usually by cloning. Read more

#### `fn clone_into(&self, target: &mut T)`[src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into` #41263)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

### `impl<T> ToString for T where
T: Display + ?Sized,` [src]

#### `default fn to_string(&self) -> String`[src]

Converts the given value to a `String`. Read more