1 // Tests for this module
2 #[cfg(all(test, not(target_os = "emscripten")))]
5 use crate::cmp::Ordering;
6 use crate::fmt::{self, Write};
7 use crate::mem::transmute;
8 use crate::sys::net::netc as c;
9 use crate::sys_common::{FromInner, IntoInner};
12 use display_buffer::IpDisplayBuffer;
14 /// An IP address, either IPv4 or IPv6.
16 /// This enum can contain either an [`Ipv4Addr`] or an [`Ipv6Addr`], see their
17 /// respective documentation for more details.
22 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
24 /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
25 /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
27 /// assert_eq!("127.0.0.1".parse(), Ok(localhost_v4));
28 /// assert_eq!("::1".parse(), Ok(localhost_v6));
30 /// assert_eq!(localhost_v4.is_ipv6(), false);
31 /// assert_eq!(localhost_v4.is_ipv4(), true);
33 #[stable(feature = "ip_addr", since = "1.7.0")]
34 #[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
37 #[stable(feature = "ip_addr", since = "1.7.0")]
38 V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
40 #[stable(feature = "ip_addr", since = "1.7.0")]
41 V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
46 /// IPv4 addresses are defined as 32-bit integers in [IETF RFC 791].
47 /// They are usually represented as four octets.
49 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
51 /// [IETF RFC 791]: https://tools.ietf.org/html/rfc791
53 /// # Textual representation
55 /// `Ipv4Addr` provides a [`FromStr`] implementation. The four octets are in decimal
56 /// notation, divided by `.` (this is called "dot-decimal notation").
57 /// Notably, octal numbers (which are indicated with a leading `0`) and hexadecimal numbers (which
58 /// are indicated with a leading `0x`) are not allowed per [IETF RFC 6943].
60 /// [IETF RFC 6943]: https://tools.ietf.org/html/rfc6943#section-3.1.1
61 /// [`FromStr`]: crate::str::FromStr
66 /// use std::net::Ipv4Addr;
68 /// let localhost = Ipv4Addr::new(127, 0, 0, 1);
69 /// assert_eq!("127.0.0.1".parse(), Ok(localhost));
70 /// assert_eq!(localhost.is_loopback(), true);
71 /// assert!("012.004.002.000".parse::<Ipv4Addr>().is_err()); // all octets are in octal
72 /// assert!("0000000.0.0.0".parse::<Ipv4Addr>().is_err()); // first octet is a zero in octal
73 /// assert!("0xcb.0x0.0x71.0x00".parse::<Ipv4Addr>().is_err()); // all octets are in hex
75 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
76 #[stable(feature = "rust1", since = "1.0.0")]
83 /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291].
84 /// They are usually represented as eight 16-bit segments.
86 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
88 /// # Embedding IPv4 Addresses
90 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
92 /// To assist in the transition from IPv4 to IPv6 two types of IPv6 addresses that embed an IPv4 address were defined:
93 /// IPv4-compatible and IPv4-mapped addresses. Of these IPv4-compatible addresses have been officially deprecated.
95 /// Both types of addresses are not assigned any special meaning by this implementation,
96 /// other than what the relevant standards prescribe. This means that an address like `::ffff:127.0.0.1`,
97 /// while representing an IPv4 loopback address, is not itself an IPv6 loopback address; only `::1` is.
98 /// To handle these so called "IPv4-in-IPv6" addresses, they have to first be converted to their canonical IPv4 address.
100 /// ### IPv4-Compatible IPv6 Addresses
102 /// IPv4-compatible IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.1], and have been officially deprecated.
103 /// The RFC describes the format of an "IPv4-Compatible IPv6 address" as follows:
106 /// | 80 bits | 16 | 32 bits |
107 /// +--------------------------------------+--------------------------+
108 /// |0000..............................0000|0000| IPv4 address |
109 /// +--------------------------------------+----+---------------------+
111 /// So `::a.b.c.d` would be an IPv4-compatible IPv6 address representing the IPv4 address `a.b.c.d`.
113 /// To convert from an IPv4 address to an IPv4-compatible IPv6 address, use [`Ipv4Addr::to_ipv6_compatible`].
114 /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-compatible IPv6 address to the canonical IPv4 address.
116 /// [IETF RFC 4291 Section 2.5.5.1]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.1
118 /// ### IPv4-Mapped IPv6 Addresses
120 /// IPv4-mapped IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.2].
121 /// The RFC describes the format of an "IPv4-Mapped IPv6 address" as follows:
124 /// | 80 bits | 16 | 32 bits |
125 /// +--------------------------------------+--------------------------+
126 /// |0000..............................0000|FFFF| IPv4 address |
127 /// +--------------------------------------+----+---------------------+
129 /// So `::ffff:a.b.c.d` would be an IPv4-mapped IPv6 address representing the IPv4 address `a.b.c.d`.
131 /// To convert from an IPv4 address to an IPv4-mapped IPv6 address, use [`Ipv4Addr::to_ipv6_mapped`].
132 /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-mapped IPv6 address to the canonical IPv4 address.
133 /// Note that this will also convert the IPv6 loopback address `::1` to `0.0.0.1`. Use
134 /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this.
136 /// [IETF RFC 4291 Section 2.5.5.2]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2
138 /// # Textual representation
140 /// `Ipv6Addr` provides a [`FromStr`] implementation. There are many ways to represent
141 /// an IPv6 address in text, but in general, each segments is written in hexadecimal
142 /// notation, and segments are separated by `:`. For more information, see
145 /// [`FromStr`]: crate::str::FromStr
146 /// [IETF RFC 5952]: https://tools.ietf.org/html/rfc5952
151 /// use std::net::Ipv6Addr;
153 /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
154 /// assert_eq!("::1".parse(), Ok(localhost));
155 /// assert_eq!(localhost.is_loopback(), true);
157 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
158 #[stable(feature = "rust1", since = "1.0.0")]
159 pub struct Ipv6Addr {
163 /// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2].
165 /// # Stability Guarantees
167 /// Not all possible values for a multicast scope have been assigned.
168 /// Future RFCs may introduce new scopes, which will be added as variants to this enum;
169 /// because of this the enum is marked as `#[non_exhaustive]`.
175 /// use std::net::Ipv6Addr;
176 /// use std::net::Ipv6MulticastScope::*;
178 /// // An IPv6 multicast address with global scope (`ff0e::`).
179 /// let address = Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0);
181 /// // Will print "Global scope".
182 /// match address.multicast_scope() {
183 /// Some(InterfaceLocal) => println!("Interface-Local scope"),
184 /// Some(LinkLocal) => println!("Link-Local scope"),
185 /// Some(RealmLocal) => println!("Realm-Local scope"),
186 /// Some(AdminLocal) => println!("Admin-Local scope"),
187 /// Some(SiteLocal) => println!("Site-Local scope"),
188 /// Some(OrganizationLocal) => println!("Organization-Local scope"),
189 /// Some(Global) => println!("Global scope"),
190 /// Some(_) => println!("Unknown scope"),
191 /// None => println!("Not a multicast address!")
196 /// [IPv6 multicast address]: Ipv6Addr
197 /// [IETF RFC 7346 section 2]: https://tools.ietf.org/html/rfc7346#section-2
198 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
199 #[unstable(feature = "ip", issue = "27709")]
201 pub enum Ipv6MulticastScope {
202 /// Interface-Local scope.
204 /// Link-Local scope.
206 /// Realm-Local scope.
208 /// Admin-Local scope.
210 /// Site-Local scope.
212 /// Organization-Local scope.
219 /// Returns [`true`] for the special 'unspecified' address.
221 /// See the documentation for [`Ipv4Addr::is_unspecified()`] and
222 /// [`Ipv6Addr::is_unspecified()`] for more details.
227 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
229 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
230 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
232 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
233 #[stable(feature = "ip_shared", since = "1.12.0")]
236 pub const fn is_unspecified(&self) -> bool {
238 IpAddr::V4(ip) => ip.is_unspecified(),
239 IpAddr::V6(ip) => ip.is_unspecified(),
243 /// Returns [`true`] if this is a loopback address.
245 /// See the documentation for [`Ipv4Addr::is_loopback()`] and
246 /// [`Ipv6Addr::is_loopback()`] for more details.
251 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
253 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
254 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
256 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
257 #[stable(feature = "ip_shared", since = "1.12.0")]
260 pub const fn is_loopback(&self) -> bool {
262 IpAddr::V4(ip) => ip.is_loopback(),
263 IpAddr::V6(ip) => ip.is_loopback(),
267 /// Returns [`true`] if the address appears to be globally routable.
269 /// See the documentation for [`Ipv4Addr::is_global()`] and
270 /// [`Ipv6Addr::is_global()`] for more details.
277 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
279 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
280 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true);
282 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
283 #[unstable(feature = "ip", issue = "27709")]
286 pub const fn is_global(&self) -> bool {
288 IpAddr::V4(ip) => ip.is_global(),
289 IpAddr::V6(ip) => ip.is_global(),
293 /// Returns [`true`] if this is a multicast address.
295 /// See the documentation for [`Ipv4Addr::is_multicast()`] and
296 /// [`Ipv6Addr::is_multicast()`] for more details.
301 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
303 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
304 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
306 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
307 #[stable(feature = "ip_shared", since = "1.12.0")]
310 pub const fn is_multicast(&self) -> bool {
312 IpAddr::V4(ip) => ip.is_multicast(),
313 IpAddr::V6(ip) => ip.is_multicast(),
317 /// Returns [`true`] if this address is in a range designated for documentation.
319 /// See the documentation for [`Ipv4Addr::is_documentation()`] and
320 /// [`Ipv6Addr::is_documentation()`] for more details.
327 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
329 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
331 /// IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_documentation(),
335 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
336 #[unstable(feature = "ip", issue = "27709")]
339 pub const fn is_documentation(&self) -> bool {
341 IpAddr::V4(ip) => ip.is_documentation(),
342 IpAddr::V6(ip) => ip.is_documentation(),
346 /// Returns [`true`] if this address is in a range designated for benchmarking.
348 /// See the documentation for [`Ipv4Addr::is_benchmarking()`] and
349 /// [`Ipv6Addr::is_benchmarking()`] for more details.
356 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
358 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255)).is_benchmarking(), true);
359 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0)).is_benchmarking(), true);
361 #[unstable(feature = "ip", issue = "27709")]
364 pub const fn is_benchmarking(&self) -> bool {
366 IpAddr::V4(ip) => ip.is_benchmarking(),
367 IpAddr::V6(ip) => ip.is_benchmarking(),
371 /// Returns [`true`] if this address is an [`IPv4` address], and [`false`]
374 /// [`IPv4` address]: IpAddr::V4
379 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
381 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
382 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false);
384 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
385 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
388 pub const fn is_ipv4(&self) -> bool {
389 matches!(self, IpAddr::V4(_))
392 /// Returns [`true`] if this address is an [`IPv6` address], and [`false`]
395 /// [`IPv6` address]: IpAddr::V6
400 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
402 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
403 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true);
405 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
406 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
409 pub const fn is_ipv6(&self) -> bool {
410 matches!(self, IpAddr::V6(_))
413 /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped IPv6 addresses, otherwise it
414 /// return `self` as-is.
420 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
422 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
423 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
424 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
427 #[must_use = "this returns the result of the operation, \
428 without modifying the original"]
429 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
430 #[unstable(feature = "ip", issue = "27709")]
431 pub const fn to_canonical(&self) -> IpAddr {
433 &v4 @ IpAddr::V4(_) => v4,
434 IpAddr::V6(v6) => v6.to_canonical(),
440 /// Creates a new IPv4 address from four eight-bit octets.
442 /// The result will represent the IP address `a`.`b`.`c`.`d`.
447 /// use std::net::Ipv4Addr;
449 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
451 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
452 #[stable(feature = "rust1", since = "1.0.0")]
455 pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
456 Ipv4Addr { octets: [a, b, c, d] }
459 /// An IPv4 address with the address pointing to localhost: `127.0.0.1`
464 /// use std::net::Ipv4Addr;
466 /// let addr = Ipv4Addr::LOCALHOST;
467 /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
469 #[stable(feature = "ip_constructors", since = "1.30.0")]
470 pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
472 /// An IPv4 address representing an unspecified address: `0.0.0.0`
474 /// This corresponds to the constant `INADDR_ANY` in other languages.
479 /// use std::net::Ipv4Addr;
481 /// let addr = Ipv4Addr::UNSPECIFIED;
482 /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
484 #[doc(alias = "INADDR_ANY")]
485 #[stable(feature = "ip_constructors", since = "1.30.0")]
486 pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
488 /// An IPv4 address representing the broadcast address: `255.255.255.255`
493 /// use std::net::Ipv4Addr;
495 /// let addr = Ipv4Addr::BROADCAST;
496 /// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
498 #[stable(feature = "ip_constructors", since = "1.30.0")]
499 pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
501 /// Returns the four eight-bit integers that make up this address.
506 /// use std::net::Ipv4Addr;
508 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
509 /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
511 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
512 #[stable(feature = "rust1", since = "1.0.0")]
515 pub const fn octets(&self) -> [u8; 4] {
519 /// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
521 /// This property is defined in _UNIX Network Programming, Second Edition_,
522 /// W. Richard Stevens, p. 891; see also [ip7].
524 /// [ip7]: https://man7.org/linux/man-pages/man7/ip.7.html
529 /// use std::net::Ipv4Addr;
531 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
532 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
534 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
535 #[stable(feature = "ip_shared", since = "1.12.0")]
538 pub const fn is_unspecified(&self) -> bool {
539 u32::from_be_bytes(self.octets) == 0
542 /// Returns [`true`] if this is a loopback address (`127.0.0.0/8`).
544 /// This property is defined by [IETF RFC 1122].
546 /// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122
551 /// use std::net::Ipv4Addr;
553 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
554 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
556 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
557 #[stable(since = "1.7.0", feature = "ip_17")]
560 pub const fn is_loopback(&self) -> bool {
561 self.octets()[0] == 127
564 /// Returns [`true`] if this is a private address.
566 /// The private address ranges are defined in [IETF RFC 1918] and include:
569 /// - `172.16.0.0/12`
570 /// - `192.168.0.0/16`
572 /// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918
577 /// use std::net::Ipv4Addr;
579 /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
580 /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
581 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
582 /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
583 /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
584 /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
585 /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
587 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
588 #[stable(since = "1.7.0", feature = "ip_17")]
591 pub const fn is_private(&self) -> bool {
592 match self.octets() {
594 [172, b, ..] if b >= 16 && b <= 31 => true,
595 [192, 168, ..] => true,
600 /// Returns [`true`] if the address is link-local (`169.254.0.0/16`).
602 /// This property is defined by [IETF RFC 3927].
604 /// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927
609 /// use std::net::Ipv4Addr;
611 /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
612 /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
613 /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
615 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
616 #[stable(since = "1.7.0", feature = "ip_17")]
619 pub const fn is_link_local(&self) -> bool {
620 matches!(self.octets(), [169, 254, ..])
623 /// Returns [`true`] if the address appears to be globally routable.
624 /// See [iana-ipv4-special-registry][ipv4-sr].
626 /// The following return [`false`]:
628 /// - private addresses (see [`Ipv4Addr::is_private()`])
629 /// - the loopback address (see [`Ipv4Addr::is_loopback()`])
630 /// - the link-local address (see [`Ipv4Addr::is_link_local()`])
631 /// - the broadcast address (see [`Ipv4Addr::is_broadcast()`])
632 /// - addresses used for documentation (see [`Ipv4Addr::is_documentation()`])
633 /// - the unspecified address (see [`Ipv4Addr::is_unspecified()`]), and the whole
634 /// `0.0.0.0/8` block
635 /// - addresses reserved for future protocols, except
636 /// `192.0.0.9/32` and `192.0.0.10/32` which are globally routable
637 /// - addresses reserved for future use (see [`Ipv4Addr::is_reserved()`]
638 /// - addresses reserved for networking devices benchmarking (see
639 /// [`Ipv4Addr::is_benchmarking()`])
641 /// [ipv4-sr]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
648 /// use std::net::Ipv4Addr;
650 /// // private addresses are not global
651 /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
652 /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
653 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
655 /// // the 0.0.0.0/8 block is not global
656 /// assert_eq!(Ipv4Addr::new(0, 1, 2, 3).is_global(), false);
657 /// // in particular, the unspecified address is not global
658 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
660 /// // the loopback address is not global
661 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_global(), false);
663 /// // link local addresses are not global
664 /// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false);
666 /// // the broadcast address is not global
667 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_global(), false);
669 /// // the address space designated for documentation is not global
670 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false);
671 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false);
672 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false);
674 /// // shared addresses are not global
675 /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false);
677 /// // addresses reserved for protocol assignment are not global
678 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_global(), false);
679 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_global(), false);
681 /// // addresses reserved for future use are not global
682 /// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false);
684 /// // addresses reserved for network devices benchmarking are not global
685 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false);
687 /// // All the other addresses are global
688 /// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true);
689 /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
691 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
692 #[unstable(feature = "ip", issue = "27709")]
695 pub const fn is_global(&self) -> bool {
696 // check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
697 // globally routable addresses in the 192.0.0.0/24 range.
698 if u32::from_be_bytes(self.octets()) == 0xc0000009
699 || u32::from_be_bytes(self.octets()) == 0xc000000a
704 && !self.is_loopback()
705 && !self.is_link_local()
706 && !self.is_broadcast()
707 && !self.is_documentation()
709 // addresses reserved for future protocols (`192.0.0.0/24`)
710 && !(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0)
711 && !self.is_reserved()
712 && !self.is_benchmarking()
713 // Make sure the address is not in 0.0.0.0/8
714 && self.octets()[0] != 0
717 /// Returns [`true`] if this address is part of the Shared Address Space defined in
718 /// [IETF RFC 6598] (`100.64.0.0/10`).
720 /// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598
726 /// use std::net::Ipv4Addr;
728 /// assert_eq!(Ipv4Addr::new(100, 64, 0, 0).is_shared(), true);
729 /// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
730 /// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
732 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
733 #[unstable(feature = "ip", issue = "27709")]
736 pub const fn is_shared(&self) -> bool {
737 self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
740 /// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for
741 /// network devices benchmarking. This range is defined in [IETF RFC 2544] as `192.18.0.0`
742 /// through `198.19.255.255` but [errata 423] corrects it to `198.18.0.0/15`.
744 /// [IETF RFC 2544]: https://tools.ietf.org/html/rfc2544
745 /// [errata 423]: https://www.rfc-editor.org/errata/eid423
751 /// use std::net::Ipv4Addr;
753 /// assert_eq!(Ipv4Addr::new(198, 17, 255, 255).is_benchmarking(), false);
754 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_benchmarking(), true);
755 /// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true);
756 /// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false);
758 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
759 #[unstable(feature = "ip", issue = "27709")]
762 pub const fn is_benchmarking(&self) -> bool {
763 self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
766 /// Returns [`true`] if this address is reserved by IANA for future use. [IETF RFC 1112]
767 /// defines the block of reserved addresses as `240.0.0.0/4`. This range normally includes the
768 /// broadcast address `255.255.255.255`, but this implementation explicitly excludes it, since
769 /// it is obviously not reserved for future use.
771 /// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112
775 /// As IANA assigns new addresses, this method will be
776 /// updated. This may result in non-reserved addresses being
777 /// treated as reserved in code that relies on an outdated version
784 /// use std::net::Ipv4Addr;
786 /// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true);
787 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true);
789 /// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false);
790 /// // The broadcast address is not considered as reserved for future use by this implementation
791 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
793 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
794 #[unstable(feature = "ip", issue = "27709")]
797 pub const fn is_reserved(&self) -> bool {
798 self.octets()[0] & 240 == 240 && !self.is_broadcast()
801 /// Returns [`true`] if this is a multicast address (`224.0.0.0/4`).
803 /// Multicast addresses have a most significant octet between `224` and `239`,
804 /// and is defined by [IETF RFC 5771].
806 /// [IETF RFC 5771]: https://tools.ietf.org/html/rfc5771
811 /// use std::net::Ipv4Addr;
813 /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
814 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
815 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
817 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
818 #[stable(since = "1.7.0", feature = "ip_17")]
821 pub const fn is_multicast(&self) -> bool {
822 self.octets()[0] >= 224 && self.octets()[0] <= 239
825 /// Returns [`true`] if this is a broadcast address (`255.255.255.255`).
827 /// A broadcast address has all octets set to `255` as defined in [IETF RFC 919].
829 /// [IETF RFC 919]: https://tools.ietf.org/html/rfc919
834 /// use std::net::Ipv4Addr;
836 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
837 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
839 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
840 #[stable(since = "1.7.0", feature = "ip_17")]
843 pub const fn is_broadcast(&self) -> bool {
844 u32::from_be_bytes(self.octets()) == u32::from_be_bytes(Self::BROADCAST.octets())
847 /// Returns [`true`] if this address is in a range designated for documentation.
849 /// This is defined in [IETF RFC 5737]:
851 /// - `192.0.2.0/24` (TEST-NET-1)
852 /// - `198.51.100.0/24` (TEST-NET-2)
853 /// - `203.0.113.0/24` (TEST-NET-3)
855 /// [IETF RFC 5737]: https://tools.ietf.org/html/rfc5737
860 /// use std::net::Ipv4Addr;
862 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
863 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
864 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
865 /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
867 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
868 #[stable(since = "1.7.0", feature = "ip_17")]
871 pub const fn is_documentation(&self) -> bool {
872 matches!(self.octets(), [192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _])
875 /// Converts this address to an [IPv4-compatible] [`IPv6` address].
877 /// `a.b.c.d` becomes `::a.b.c.d`
879 /// Note that IPv4-compatible addresses have been officially deprecated.
880 /// If you don't explicitly need an IPv4-compatible address for legacy reasons, consider using `to_ipv6_mapped` instead.
882 /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
883 /// [`IPv6` address]: Ipv6Addr
888 /// use std::net::{Ipv4Addr, Ipv6Addr};
891 /// Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
892 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x2ff)
895 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
896 #[stable(feature = "rust1", since = "1.0.0")]
897 #[must_use = "this returns the result of the operation, \
898 without modifying the original"]
900 pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
901 let [a, b, c, d] = self.octets();
902 Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] }
905 /// Converts this address to an [IPv4-mapped] [`IPv6` address].
907 /// `a.b.c.d` becomes `::ffff:a.b.c.d`
909 /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
910 /// [`IPv6` address]: Ipv6Addr
915 /// use std::net::{Ipv4Addr, Ipv6Addr};
917 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
918 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff));
920 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
921 #[stable(feature = "rust1", since = "1.0.0")]
922 #[must_use = "this returns the result of the operation, \
923 without modifying the original"]
925 pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
926 let [a, b, c, d] = self.octets();
927 Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d] }
931 #[stable(feature = "ip_addr", since = "1.7.0")]
932 impl fmt::Display for IpAddr {
933 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
935 IpAddr::V4(ip) => ip.fmt(fmt),
936 IpAddr::V6(ip) => ip.fmt(fmt),
941 #[stable(feature = "ip_addr", since = "1.7.0")]
942 impl fmt::Debug for IpAddr {
943 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
944 fmt::Display::fmt(self, fmt)
948 #[stable(feature = "ip_from_ip", since = "1.16.0")]
949 impl From<Ipv4Addr> for IpAddr {
950 /// Copies this address to a new `IpAddr::V4`.
955 /// use std::net::{IpAddr, Ipv4Addr};
957 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
960 /// IpAddr::V4(addr),
961 /// IpAddr::from(addr)
965 fn from(ipv4: Ipv4Addr) -> IpAddr {
970 #[stable(feature = "ip_from_ip", since = "1.16.0")]
971 impl From<Ipv6Addr> for IpAddr {
972 /// Copies this address to a new `IpAddr::V6`.
977 /// use std::net::{IpAddr, Ipv6Addr};
979 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
982 /// IpAddr::V6(addr),
983 /// IpAddr::from(addr)
987 fn from(ipv6: Ipv6Addr) -> IpAddr {
992 #[stable(feature = "rust1", since = "1.0.0")]
993 impl fmt::Display for Ipv4Addr {
994 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
995 let octets = self.octets();
997 // If there are no alignment requirements, write the IP address directly to `f`.
998 // Otherwise, write it to a local buffer and then use `f.pad`.
999 if fmt.precision().is_none() && fmt.width().is_none() {
1000 write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
1002 const LONGEST_IPV4_ADDR: &str = "255.255.255.255";
1004 let mut buf = IpDisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new();
1005 // Buffer is long enough for the longest possible IPv4 address, so this should never fail.
1006 write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
1008 fmt.pad(buf.as_str())
1013 #[stable(feature = "rust1", since = "1.0.0")]
1014 impl fmt::Debug for Ipv4Addr {
1015 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1016 fmt::Display::fmt(self, fmt)
1020 #[stable(feature = "ip_cmp", since = "1.16.0")]
1021 impl PartialEq<Ipv4Addr> for IpAddr {
1023 fn eq(&self, other: &Ipv4Addr) -> bool {
1025 IpAddr::V4(v4) => v4 == other,
1026 IpAddr::V6(_) => false,
1031 #[stable(feature = "ip_cmp", since = "1.16.0")]
1032 impl PartialEq<IpAddr> for Ipv4Addr {
1034 fn eq(&self, other: &IpAddr) -> bool {
1036 IpAddr::V4(v4) => self == v4,
1037 IpAddr::V6(_) => false,
1042 #[stable(feature = "rust1", since = "1.0.0")]
1043 impl PartialOrd for Ipv4Addr {
1045 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1046 Some(self.cmp(other))
1050 #[stable(feature = "ip_cmp", since = "1.16.0")]
1051 impl PartialOrd<Ipv4Addr> for IpAddr {
1053 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1055 IpAddr::V4(v4) => v4.partial_cmp(other),
1056 IpAddr::V6(_) => Some(Ordering::Greater),
1061 #[stable(feature = "ip_cmp", since = "1.16.0")]
1062 impl PartialOrd<IpAddr> for Ipv4Addr {
1064 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1066 IpAddr::V4(v4) => self.partial_cmp(v4),
1067 IpAddr::V6(_) => Some(Ordering::Less),
1072 #[stable(feature = "rust1", since = "1.0.0")]
1073 impl Ord for Ipv4Addr {
1075 fn cmp(&self, other: &Ipv4Addr) -> Ordering {
1076 self.octets.cmp(&other.octets)
1080 impl IntoInner<c::in_addr> for Ipv4Addr {
1082 fn into_inner(self) -> c::in_addr {
1083 // `s_addr` is stored as BE on all machines and the array is in BE order.
1084 // So the native endian conversion method is used so that it's never swapped.
1085 c::in_addr { s_addr: u32::from_ne_bytes(self.octets) }
1088 impl FromInner<c::in_addr> for Ipv4Addr {
1089 fn from_inner(addr: c::in_addr) -> Ipv4Addr {
1090 Ipv4Addr { octets: addr.s_addr.to_ne_bytes() }
1094 #[stable(feature = "ip_u32", since = "1.1.0")]
1095 impl From<Ipv4Addr> for u32 {
1096 /// Converts an `Ipv4Addr` into a host byte order `u32`.
1101 /// use std::net::Ipv4Addr;
1103 /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
1104 /// assert_eq!(0x12345678, u32::from(addr));
1107 fn from(ip: Ipv4Addr) -> u32 {
1108 u32::from_be_bytes(ip.octets)
1112 #[stable(feature = "ip_u32", since = "1.1.0")]
1113 impl From<u32> for Ipv4Addr {
1114 /// Converts a host byte order `u32` into an `Ipv4Addr`.
1119 /// use std::net::Ipv4Addr;
1121 /// let addr = Ipv4Addr::from(0x12345678);
1122 /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr);
1125 fn from(ip: u32) -> Ipv4Addr {
1126 Ipv4Addr { octets: ip.to_be_bytes() }
1130 #[stable(feature = "from_slice_v4", since = "1.9.0")]
1131 impl From<[u8; 4]> for Ipv4Addr {
1132 /// Creates an `Ipv4Addr` from a four element byte array.
1137 /// use std::net::Ipv4Addr;
1139 /// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
1140 /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
1143 fn from(octets: [u8; 4]) -> Ipv4Addr {
1148 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1149 impl From<[u8; 4]> for IpAddr {
1150 /// Creates an `IpAddr::V4` from a four element byte array.
1155 /// use std::net::{IpAddr, Ipv4Addr};
1157 /// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]);
1158 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr);
1161 fn from(octets: [u8; 4]) -> IpAddr {
1162 IpAddr::V4(Ipv4Addr::from(octets))
1167 /// Creates a new IPv6 address from eight 16-bit segments.
1169 /// The result will represent the IP address `a:b:c:d:e:f:g:h`.
1174 /// use std::net::Ipv6Addr;
1176 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
1178 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
1179 #[stable(feature = "rust1", since = "1.0.0")]
1182 pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
1194 // All elements in `addr16` are big endian.
1195 // SAFETY: `[u16; 8]` is always safe to transmute to `[u8; 16]`.
1196 octets: unsafe { transmute::<_, [u8; 16]>(addr16) },
1200 /// An IPv6 address representing localhost: `::1`.
1205 /// use std::net::Ipv6Addr;
1207 /// let addr = Ipv6Addr::LOCALHOST;
1208 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1210 #[stable(feature = "ip_constructors", since = "1.30.0")]
1211 pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
1213 /// An IPv6 address representing the unspecified address: `::`
1218 /// use std::net::Ipv6Addr;
1220 /// let addr = Ipv6Addr::UNSPECIFIED;
1221 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1223 #[stable(feature = "ip_constructors", since = "1.30.0")]
1224 pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
1226 /// Returns the eight 16-bit segments that make up this address.
1231 /// use std::net::Ipv6Addr;
1233 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
1234 /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
1236 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1237 #[stable(feature = "rust1", since = "1.0.0")]
1240 pub const fn segments(&self) -> [u16; 8] {
1241 // All elements in `self.octets` must be big endian.
1242 // SAFETY: `[u8; 16]` is always safe to transmute to `[u16; 8]`.
1243 let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(self.octets) };
1244 // We want native endian u16
1257 /// Returns [`true`] for the special 'unspecified' address (`::`).
1259 /// This property is defined in [IETF RFC 4291].
1261 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1266 /// use std::net::Ipv6Addr;
1268 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
1269 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
1271 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1272 #[stable(since = "1.7.0", feature = "ip_17")]
1275 pub const fn is_unspecified(&self) -> bool {
1276 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
1279 /// Returns [`true`] if this is the [loopback address] (`::1`),
1280 /// as defined in [IETF RFC 4291 section 2.5.3].
1282 /// Contrary to IPv4, in IPv6 there is only one loopback address.
1284 /// [loopback address]: Ipv6Addr::LOCALHOST
1285 /// [IETF RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
1290 /// use std::net::Ipv6Addr;
1292 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
1293 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
1295 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1296 #[stable(since = "1.7.0", feature = "ip_17")]
1299 pub const fn is_loopback(&self) -> bool {
1300 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
1303 /// Returns [`true`] if the address appears to be globally routable.
1305 /// The following return [`false`]:
1307 /// - the loopback address
1308 /// - link-local and unique local unicast addresses
1309 /// - interface-, link-, realm-, admin- and site-local multicast addresses
1316 /// use std::net::Ipv6Addr;
1318 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
1319 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
1320 /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
1322 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1323 #[unstable(feature = "ip", issue = "27709")]
1326 pub const fn is_global(&self) -> bool {
1327 match self.multicast_scope() {
1328 Some(Ipv6MulticastScope::Global) => true,
1329 None => self.is_unicast_global(),
1334 /// Returns [`true`] if this is a unique local address (`fc00::/7`).
1336 /// This property is defined in [IETF RFC 4193].
1338 /// [IETF RFC 4193]: https://tools.ietf.org/html/rfc4193
1345 /// use std::net::Ipv6Addr;
1347 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
1348 /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
1350 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1351 #[unstable(feature = "ip", issue = "27709")]
1354 pub const fn is_unique_local(&self) -> bool {
1355 (self.segments()[0] & 0xfe00) == 0xfc00
1358 /// Returns [`true`] if this is a unicast address, as defined by [IETF RFC 4291].
1359 /// Any address that is not a [multicast address] (`ff00::/8`) is unicast.
1361 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1362 /// [multicast address]: Ipv6Addr::is_multicast
1369 /// use std::net::Ipv6Addr;
1371 /// // The unspecified and loopback addresses are unicast.
1372 /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast(), true);
1373 /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast(), true);
1375 /// // Any address that is not a multicast address (`ff00::/8`) is unicast.
1376 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast(), true);
1377 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_unicast(), false);
1379 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1380 #[unstable(feature = "ip", issue = "27709")]
1383 pub const fn is_unicast(&self) -> bool {
1384 !self.is_multicast()
1387 /// Returns `true` if the address is a unicast address with link-local scope,
1388 /// as defined in [RFC 4291].
1390 /// A unicast address has link-local scope if it has the prefix `fe80::/10`, as per [RFC 4291 section 2.4].
1391 /// Note that this encompasses more addresses than those defined in [RFC 4291 section 2.5.6],
1392 /// which describes "Link-Local IPv6 Unicast Addresses" as having the following stricter format:
1395 /// | 10 bits | 54 bits | 64 bits |
1396 /// +----------+-------------------------+----------------------------+
1397 /// |1111111010| 0 | interface ID |
1398 /// +----------+-------------------------+----------------------------+
1400 /// So while currently the only addresses with link-local scope an application will encounter are all in `fe80::/64`,
1401 /// this might change in the future with the publication of new standards. More addresses in `fe80::/10` could be allocated,
1402 /// and those addresses will have link-local scope.
1404 /// 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",
1405 /// this does not mean that the loopback address actually has link-local scope and this method will return `false` on it.
1407 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
1408 /// [RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
1409 /// [RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
1410 /// [RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6
1411 /// [loopback address]: Ipv6Addr::LOCALHOST
1418 /// use std::net::Ipv6Addr;
1420 /// // The loopback address (`::1`) does not actually have link-local scope.
1421 /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast_link_local(), false);
1423 /// // Only addresses in `fe80::/10` have link-local scope.
1424 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), false);
1425 /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
1427 /// // Addresses outside the stricter `fe80::/64` also have link-local scope.
1428 /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0).is_unicast_link_local(), true);
1429 /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
1431 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1432 #[unstable(feature = "ip", issue = "27709")]
1435 pub const fn is_unicast_link_local(&self) -> bool {
1436 (self.segments()[0] & 0xffc0) == 0xfe80
1439 /// Returns [`true`] if this is an address reserved for documentation
1440 /// (`2001:db8::/32`).
1442 /// This property is defined in [IETF RFC 3849].
1444 /// [IETF RFC 3849]: https://tools.ietf.org/html/rfc3849
1451 /// use std::net::Ipv6Addr;
1453 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
1454 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
1456 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1457 #[unstable(feature = "ip", issue = "27709")]
1460 pub const fn is_documentation(&self) -> bool {
1461 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
1464 /// Returns [`true`] if this is an address reserved for benchmarking (`2001:2::/48`).
1466 /// This property is defined in [IETF RFC 5180], where it is mistakenly specified as covering the range `2001:0200::/48`.
1467 /// This is corrected in [IETF RFC Errata 1752] to `2001:0002::/48`.
1469 /// [IETF RFC 5180]: https://tools.ietf.org/html/rfc5180
1470 /// [IETF RFC Errata 1752]: https://www.rfc-editor.org/errata_search.php?eid=1752
1475 /// use std::net::Ipv6Addr;
1477 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc613, 0x0).is_benchmarking(), false);
1478 /// assert_eq!(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0).is_benchmarking(), true);
1480 #[unstable(feature = "ip", issue = "27709")]
1483 pub const fn is_benchmarking(&self) -> bool {
1484 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) && (self.segments()[2] == 0)
1487 /// Returns [`true`] if the address is a globally routable unicast address.
1489 /// The following return false:
1491 /// - the loopback address
1492 /// - the link-local addresses
1493 /// - unique local addresses
1494 /// - the unspecified address
1495 /// - the address range reserved for documentation
1497 /// This method returns [`true`] for site-local addresses as per [RFC 4291 section 2.5.7]
1500 /// The special behavior of [the site-local unicast] prefix defined in [RFC3513] must no longer
1501 /// be supported in new implementations (i.e., new implementations must treat this prefix as
1502 /// Global Unicast).
1505 /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
1512 /// use std::net::Ipv6Addr;
1514 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1515 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
1517 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1518 #[unstable(feature = "ip", issue = "27709")]
1521 pub const fn is_unicast_global(&self) -> bool {
1523 && !self.is_loopback()
1524 && !self.is_unicast_link_local()
1525 && !self.is_unique_local()
1526 && !self.is_unspecified()
1527 && !self.is_documentation()
1530 /// Returns the address's multicast scope if the address is multicast.
1537 /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
1540 /// Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
1541 /// Some(Ipv6MulticastScope::Global)
1543 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
1545 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1546 #[unstable(feature = "ip", issue = "27709")]
1549 pub const fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
1550 if self.is_multicast() {
1551 match self.segments()[0] & 0x000f {
1552 1 => Some(Ipv6MulticastScope::InterfaceLocal),
1553 2 => Some(Ipv6MulticastScope::LinkLocal),
1554 3 => Some(Ipv6MulticastScope::RealmLocal),
1555 4 => Some(Ipv6MulticastScope::AdminLocal),
1556 5 => Some(Ipv6MulticastScope::SiteLocal),
1557 8 => Some(Ipv6MulticastScope::OrganizationLocal),
1558 14 => Some(Ipv6MulticastScope::Global),
1566 /// Returns [`true`] if this is a multicast address (`ff00::/8`).
1568 /// This property is defined by [IETF RFC 4291].
1570 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1575 /// use std::net::Ipv6Addr;
1577 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
1578 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
1580 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1581 #[stable(since = "1.7.0", feature = "ip_17")]
1584 pub const fn is_multicast(&self) -> bool {
1585 (self.segments()[0] & 0xff00) == 0xff00
1588 /// Converts this address to an [`IPv4` address] if it's an [IPv4-mapped] address,
1589 /// as defined in [IETF RFC 4291 section 2.5.5.2], otherwise returns [`None`].
1591 /// `::ffff:a.b.c.d` becomes `a.b.c.d`.
1592 /// All addresses *not* starting with `::ffff` will return `None`.
1594 /// [`IPv4` address]: Ipv4Addr
1595 /// [IPv4-mapped]: Ipv6Addr
1596 /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
1601 /// use std::net::{Ipv4Addr, Ipv6Addr};
1603 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4_mapped(), None);
1604 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4_mapped(),
1605 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
1606 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
1608 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1609 #[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
1610 #[must_use = "this returns the result of the operation, \
1611 without modifying the original"]
1613 pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
1614 match self.octets() {
1615 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
1616 Some(Ipv4Addr::new(a, b, c, d))
1622 /// Converts this address to an [`IPv4` address] if it is either
1623 /// an [IPv4-compatible] address as defined in [IETF RFC 4291 section 2.5.5.1],
1624 /// or an [IPv4-mapped] address as defined in [IETF RFC 4291 section 2.5.5.2],
1625 /// otherwise returns [`None`].
1627 /// Note that this will return an [`IPv4` address] for the IPv6 loopback address `::1`. Use
1628 /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this.
1630 /// `::a.b.c.d` and `::ffff:a.b.c.d` become `a.b.c.d`. `::1` becomes `0.0.0.1`.
1631 /// All addresses *not* starting with either all zeroes or `::ffff` will return `None`.
1633 /// [`IPv4` address]: Ipv4Addr
1634 /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
1635 /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
1636 /// [IETF RFC 4291 section 2.5.5.1]: https://tools.ietf.org/html/rfc4291#section-2.5.5.1
1637 /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
1642 /// use std::net::{Ipv4Addr, Ipv6Addr};
1644 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
1645 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
1646 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
1647 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
1648 /// Some(Ipv4Addr::new(0, 0, 0, 1)));
1650 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1651 #[stable(feature = "rust1", since = "1.0.0")]
1652 #[must_use = "this returns the result of the operation, \
1653 without modifying the original"]
1655 pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
1656 if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
1657 let [a, b] = ab.to_be_bytes();
1658 let [c, d] = cd.to_be_bytes();
1659 Some(Ipv4Addr::new(a, b, c, d))
1665 /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped addresses, otherwise it
1666 /// returns self wrapped in an `IpAddr::V6`.
1672 /// use std::net::Ipv6Addr;
1674 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
1675 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
1677 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1678 #[unstable(feature = "ip", issue = "27709")]
1679 #[must_use = "this returns the result of the operation, \
1680 without modifying the original"]
1682 pub const fn to_canonical(&self) -> IpAddr {
1683 if let Some(mapped) = self.to_ipv4_mapped() {
1684 return IpAddr::V4(mapped);
1689 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
1692 /// use std::net::Ipv6Addr;
1694 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
1695 /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1697 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
1698 #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
1701 pub const fn octets(&self) -> [u8; 16] {
1706 /// Write an Ipv6Addr, conforming to the canonical style described by
1707 /// [RFC 5952](https://tools.ietf.org/html/rfc5952).
1708 #[stable(feature = "rust1", since = "1.0.0")]
1709 impl fmt::Display for Ipv6Addr {
1710 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1711 // If there are no alignment requirements, write the IP address directly to `f`.
1712 // Otherwise, write it to a local buffer and then use `f.pad`.
1713 if f.precision().is_none() && f.width().is_none() {
1714 let segments = self.segments();
1716 // Special case for :: and ::1; otherwise they get written with the
1718 if self.is_unspecified() {
1720 } else if self.is_loopback() {
1722 } else if let Some(ipv4) = self.to_ipv4() {
1724 // IPv4 Compatible address
1725 0 => write!(f, "::{}", ipv4),
1726 // IPv4 Mapped address
1727 0xffff => write!(f, "::ffff:{}", ipv4),
1728 _ => unreachable!(),
1731 #[derive(Copy, Clone, Default)]
1737 // Find the inner 0 span
1739 let mut longest = Span::default();
1740 let mut current = Span::default();
1742 for (i, &segment) in segments.iter().enumerate() {
1744 if current.len == 0 {
1750 if current.len > longest.len {
1754 current = Span::default();
1761 /// Write a colon-separated part of the address
1763 fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result {
1764 if let Some((first, tail)) = chunk.split_first() {
1765 write!(f, "{:x}", first)?;
1766 for segment in tail {
1768 write!(f, "{:x}", segment)?;
1775 fmt_subslice(f, &segments[..zeroes.start])?;
1777 fmt_subslice(f, &segments[zeroes.start + zeroes.len..])
1779 fmt_subslice(f, &segments)
1783 const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";
1785 let mut buf = IpDisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new();
1786 // Buffer is long enough for the longest possible IPv6 address, so this should never fail.
1787 write!(buf, "{}", self).unwrap();
1794 #[stable(feature = "rust1", since = "1.0.0")]
1795 impl fmt::Debug for Ipv6Addr {
1796 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1797 fmt::Display::fmt(self, fmt)
1801 #[stable(feature = "ip_cmp", since = "1.16.0")]
1802 impl PartialEq<IpAddr> for Ipv6Addr {
1804 fn eq(&self, other: &IpAddr) -> bool {
1806 IpAddr::V4(_) => false,
1807 IpAddr::V6(v6) => self == v6,
1812 #[stable(feature = "ip_cmp", since = "1.16.0")]
1813 impl PartialEq<Ipv6Addr> for IpAddr {
1815 fn eq(&self, other: &Ipv6Addr) -> bool {
1817 IpAddr::V4(_) => false,
1818 IpAddr::V6(v6) => v6 == other,
1823 #[stable(feature = "rust1", since = "1.0.0")]
1824 impl PartialOrd for Ipv6Addr {
1826 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1827 Some(self.cmp(other))
1831 #[stable(feature = "ip_cmp", since = "1.16.0")]
1832 impl PartialOrd<Ipv6Addr> for IpAddr {
1834 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1836 IpAddr::V4(_) => Some(Ordering::Less),
1837 IpAddr::V6(v6) => v6.partial_cmp(other),
1842 #[stable(feature = "ip_cmp", since = "1.16.0")]
1843 impl PartialOrd<IpAddr> for Ipv6Addr {
1845 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1847 IpAddr::V4(_) => Some(Ordering::Greater),
1848 IpAddr::V6(v6) => self.partial_cmp(v6),
1853 #[stable(feature = "rust1", since = "1.0.0")]
1854 impl Ord for Ipv6Addr {
1856 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1857 self.segments().cmp(&other.segments())
1861 impl IntoInner<c::in6_addr> for Ipv6Addr {
1862 fn into_inner(self) -> c::in6_addr {
1863 c::in6_addr { s6_addr: self.octets }
1866 impl FromInner<c::in6_addr> for Ipv6Addr {
1868 fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1869 Ipv6Addr { octets: addr.s6_addr }
1873 #[stable(feature = "i128", since = "1.26.0")]
1874 impl From<Ipv6Addr> for u128 {
1875 /// Convert an `Ipv6Addr` into a host byte order `u128`.
1880 /// use std::net::Ipv6Addr;
1882 /// let addr = Ipv6Addr::new(
1883 /// 0x1020, 0x3040, 0x5060, 0x7080,
1884 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1886 /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1889 fn from(ip: Ipv6Addr) -> u128 {
1890 u128::from_be_bytes(ip.octets)
1893 #[stable(feature = "i128", since = "1.26.0")]
1894 impl From<u128> for Ipv6Addr {
1895 /// Convert a host byte order `u128` into an `Ipv6Addr`.
1900 /// use std::net::Ipv6Addr;
1902 /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1905 /// 0x1020, 0x3040, 0x5060, 0x7080,
1906 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1911 fn from(ip: u128) -> Ipv6Addr {
1912 Ipv6Addr::from(ip.to_be_bytes())
1916 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
1917 impl From<[u8; 16]> for Ipv6Addr {
1918 /// Creates an `Ipv6Addr` from a sixteen element byte array.
1923 /// use std::net::Ipv6Addr;
1925 /// let addr = Ipv6Addr::from([
1926 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
1927 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
1940 fn from(octets: [u8; 16]) -> Ipv6Addr {
1945 #[stable(feature = "ipv6_from_segments", since = "1.16.0")]
1946 impl From<[u16; 8]> for Ipv6Addr {
1947 /// Creates an `Ipv6Addr` from an eight element 16-bit array.
1952 /// use std::net::Ipv6Addr;
1954 /// let addr = Ipv6Addr::from([
1955 /// 525u16, 524u16, 523u16, 522u16,
1956 /// 521u16, 520u16, 519u16, 518u16,
1969 fn from(segments: [u16; 8]) -> Ipv6Addr {
1970 let [a, b, c, d, e, f, g, h] = segments;
1971 Ipv6Addr::new(a, b, c, d, e, f, g, h)
1975 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1976 impl From<[u8; 16]> for IpAddr {
1977 /// Creates an `IpAddr::V6` from a sixteen element byte array.
1982 /// use std::net::{IpAddr, Ipv6Addr};
1984 /// let addr = IpAddr::from([
1985 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
1986 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
1989 /// IpAddr::V6(Ipv6Addr::new(
1999 fn from(octets: [u8; 16]) -> IpAddr {
2000 IpAddr::V6(Ipv6Addr::from(octets))
2004 #[stable(feature = "ip_from_slice", since = "1.17.0")]
2005 impl From<[u16; 8]> for IpAddr {
2006 /// Creates an `IpAddr::V6` from an eight element 16-bit array.
2011 /// use std::net::{IpAddr, Ipv6Addr};
2013 /// let addr = IpAddr::from([
2014 /// 525u16, 524u16, 523u16, 522u16,
2015 /// 521u16, 520u16, 519u16, 518u16,
2018 /// IpAddr::V6(Ipv6Addr::new(
2028 fn from(segments: [u16; 8]) -> IpAddr {
2029 IpAddr::V6(Ipv6Addr::from(segments))