@@ -522,6 +522,14 @@ impl Ipv6Addr {
522522 /// Creates a new IPv6 address from eight 16-bit segments.
523523 ///
524524 /// The result will represent the IP address a:b:c:d:e:f:g:h.
525+ ///
526+ /// # Examples
527+ ///
528+ /// ```
529+ /// use std::net::Ipv6Addr;
530+ ///
531+ /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
532+ /// ```
525533 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
526534 pub fn new ( a : u16 , b : u16 , c : u16 , d : u16 , e : u16 , f : u16 , g : u16 ,
527535 h : u16 ) -> Ipv6Addr {
@@ -538,6 +546,15 @@ impl Ipv6Addr {
538546 }
539547
540548 /// Returns the eight 16-bit segments that make up this address.
549+ ///
550+ /// # Examples
551+ ///
552+ /// ```
553+ /// use std::net::Ipv6Addr;
554+ ///
555+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
556+ /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
557+ /// ```
541558 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
542559 pub fn segments ( & self ) -> [ u16 ; 8 ] {
543560 let arr = & self . inner . s6_addr ;
@@ -558,6 +575,15 @@ impl Ipv6Addr {
558575 /// This property is defined in [RFC 4291].
559576 ///
560577 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
578+ ///
579+ /// # Examples
580+ ///
581+ /// ```
582+ /// use std::net::Ipv6Addr;
583+ ///
584+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
585+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
586+ /// ```
561587 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
562588 pub fn is_unspecified ( & self ) -> bool {
563589 self . segments ( ) == [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
@@ -568,6 +594,15 @@ impl Ipv6Addr {
568594 /// This property is defined in [RFC 4291].
569595 ///
570596 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
597+ ///
598+ /// # Examples
599+ ///
600+ /// ```
601+ /// use std::net::Ipv6Addr;
602+ ///
603+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
604+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
605+ /// ```
571606 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
572607 pub fn is_loopback ( & self ) -> bool {
573608 self . segments ( ) == [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ]
@@ -580,6 +615,20 @@ impl Ipv6Addr {
580615 /// - the loopback address
581616 /// - link-local, site-local, and unique local unicast addresses
582617 /// - interface-, link-, realm-, admin- and site-local multicast addresses
618+ ///
619+ /// # Examples
620+ ///
621+ /// ```
622+ /// #![feature(ip)]
623+ ///
624+ /// use std::net::Ipv6Addr;
625+ ///
626+ /// fn main() {
627+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
628+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
629+ /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
630+ /// }
631+ /// ```
583632 pub fn is_global ( & self ) -> bool {
584633 match self . multicast_scope ( ) {
585634 Some ( Ipv6MulticastScope :: Global ) => true ,
@@ -593,6 +642,15 @@ impl Ipv6Addr {
593642 /// This property is defined in [RFC 4193].
594643 ///
595644 /// [RFC 4193]: https://tools.ietf.org/html/rfc4193
645+ ///
646+ /// # Examples
647+ ///
648+ /// ```
649+ /// use std::net::Ipv6Addr;
650+ ///
651+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
652+ /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
653+ /// ```
596654 pub fn is_unique_local ( & self ) -> bool {
597655 ( self . segments ( ) [ 0 ] & 0xfe00 ) == 0xfc00
598656 }
@@ -602,12 +660,32 @@ impl Ipv6Addr {
602660 /// This property is defined in [RFC 4291].
603661 ///
604662 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
663+ ///
664+ /// # Examples
665+ ///
666+ /// ```
667+ /// use std::net::Ipv6Addr;
668+ ///
669+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_link_local(),
670+ /// false);
671+ /// assert_eq!(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
672+ /// ```
605673 pub fn is_unicast_link_local ( & self ) -> bool {
606674 ( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfe80
607675 }
608676
609677 /// Returns true if this is a deprecated unicast site-local address
610678 /// (fec0::/10).
679+ ///
680+ /// # Examples
681+ ///
682+ /// ```
683+ /// use std::net::Ipv6Addr;
684+ ///
685+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
686+ /// false);
687+ /// assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
688+ /// ```
611689 pub fn is_unicast_site_local ( & self ) -> bool {
612690 ( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfec0
613691 }
@@ -618,6 +696,15 @@ impl Ipv6Addr {
618696 /// This property is defined in [RFC 3849].
619697 ///
620698 /// [RFC 3849]: https://tools.ietf.org/html/rfc3849
699+ ///
700+ /// # Examples
701+ ///
702+ /// ```
703+ /// use std::net::Ipv6Addr;
704+ ///
705+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
706+ /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
707+ /// ```
621708 pub fn is_documentation ( & self ) -> bool {
622709 ( self . segments ( ) [ 0 ] == 0x2001 ) && ( self . segments ( ) [ 1 ] == 0xdb8 )
623710 }
@@ -632,6 +719,15 @@ impl Ipv6Addr {
632719 /// - unique local addresses
633720 /// - the unspecified address
634721 /// - the address range reserved for documentation
722+ ///
723+ /// # Examples
724+ ///
725+ /// ```
726+ /// use std::net::Ipv6Addr;
727+ ///
728+ /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
729+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
730+ /// ```
635731 pub fn is_unicast_global ( & self ) -> bool {
636732 !self . is_multicast ( )
637733 && !self . is_loopback ( ) && !self . is_unicast_link_local ( )
@@ -640,6 +736,16 @@ impl Ipv6Addr {
640736 }
641737
642738 /// Returns the address's multicast scope if the address is multicast.
739+ ///
740+ /// # Examples
741+ ///
742+ /// ```
743+ /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
744+ ///
745+ /// assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
746+ /// Some(Ipv6MulticastScope::Global));
747+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
748+ /// ```
643749 pub fn multicast_scope ( & self ) -> Option < Ipv6MulticastScope > {
644750 if self . is_multicast ( ) {
645751 match self . segments ( ) [ 0 ] & 0x000f {
@@ -662,6 +768,14 @@ impl Ipv6Addr {
662768 /// This property is defined by [RFC 4291].
663769 ///
664770 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
771+ /// # Examples
772+ ///
773+ /// ```
774+ /// use std::net::Ipv6Addr;
775+ ///
776+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
777+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
778+ /// ```
665779 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
666780 pub fn is_multicast ( & self ) -> bool {
667781 ( self . segments ( ) [ 0 ] & 0xff00 ) == 0xff00
@@ -671,6 +785,14 @@ impl Ipv6Addr {
671785 /// neither IPv4-compatible or IPv4-mapped.
672786 ///
673787 /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
788+ ///
789+ /// ```
790+ /// use std::net::{Ipv4Addr, Ipv6Addr};
791+ ///
792+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
793+ /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
794+ /// Some(Ipv4Addr::new(192, 10, 2, 255)));
795+ /// ```
674796 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
675797 pub fn to_ipv4 ( & self ) -> Option < Ipv4Addr > {
676798 match self . segments ( ) {
@@ -683,6 +805,13 @@ impl Ipv6Addr {
683805 }
684806
685807 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
808+ ///
809+ /// ```
810+ /// use std::net::Ipv6Addr;
811+ ///
812+ /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
813+ /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
814+ /// ```
686815 #[ stable( feature = "ipv6_to_octets" , since = "1.12.0" ) ]
687816 pub fn octets ( & self ) -> [ u8 ; 16 ] {
688817 self . inner . s6_addr
0 commit comments