@@ -154,6 +154,14 @@ impl Ipv4Addr {
154154 /// Creates a new IPv4 address from four eight-bit octets.
155155 ///
156156 /// The result will represent the IP address `a`.`b`.`c`.`d`.
157+ ///
158+ /// # Examples
159+ ///
160+ /// ```
161+ /// use std::net::Ipv4Addr;
162+ ///
163+ /// let addr = Ipv4Addr::new(127, 0, 0, 1);
164+ /// ```
157165 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
158166 pub fn new ( a : u8 , b : u8 , c : u8 , d : u8 ) -> Ipv4Addr {
159167 Ipv4Addr {
@@ -167,6 +175,15 @@ impl Ipv4Addr {
167175 }
168176
169177 /// Returns the four eight-bit integers that make up this address.
178+ ///
179+ /// # Examples
180+ ///
181+ /// ```
182+ /// use std::net::Ipv4Addr;
183+ ///
184+ /// let addr = Ipv4Addr::new(127, 0, 0, 1);
185+ /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
186+ /// ```
170187 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
171188 pub fn octets ( & self ) -> [ u8 ; 4 ] {
172189 let bits = ntoh ( self . inner . s_addr ) ;
@@ -176,8 +193,18 @@ impl Ipv4Addr {
176193 /// Returns true for the special 'unspecified' address (0.0.0.0).
177194 ///
178195 /// This property is defined in _UNIX Network Programming, Second Edition_,
179- /// W. Richard Stevens, p. 891; see also [ip7]
180- /// [ip7][http://man7.org/linux/man-pages/man7/ip.7.html]
196+ /// W. Richard Stevens, p. 891; see also [ip7].
197+ ///
198+ /// [ip7]: http://man7.org/linux/man-pages/man7/ip.7.html
199+ ///
200+ /// # Examples
201+ ///
202+ /// ```
203+ /// use std::net::Ipv4Addr;
204+ ///
205+ /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
206+ /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
207+ /// ```
181208 #[ stable( feature = "ip_shared" , since = "1.12.0" ) ]
182209 pub fn is_unspecified ( & self ) -> bool {
183210 self . inner . s_addr == 0
@@ -186,7 +213,17 @@ impl Ipv4Addr {
186213 /// Returns true if this is a loopback address (127.0.0.0/8).
187214 ///
188215 /// This property is defined by [RFC 1122].
216+ ///
189217 /// [RFC 1122]: https://tools.ietf.org/html/rfc1122
218+ ///
219+ /// # Examples
220+ ///
221+ /// ```
222+ /// use std::net::Ipv4Addr;
223+ ///
224+ /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
225+ /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
226+ /// ```
190227 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
191228 pub fn is_loopback ( & self ) -> bool {
192229 self . octets ( ) [ 0 ] == 127
@@ -195,11 +232,26 @@ impl Ipv4Addr {
195232 /// Returns true if this is a private address.
196233 ///
197234 /// The private address ranges are defined in [RFC 1918] and include:
198- /// [RFC 1918]: https://tools.ietf.org/html/rfc1918
199235 ///
200236 /// - 10.0.0.0/8
201237 /// - 172.16.0.0/12
202238 /// - 192.168.0.0/16
239+ ///
240+ /// [RFC 1918]: https://tools.ietf.org/html/rfc1918
241+ ///
242+ /// # Examples
243+ ///
244+ /// ```
245+ /// use std::net::Ipv4Addr;
246+ ///
247+ /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
248+ /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
249+ /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
250+ /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
251+ /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
252+ /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
253+ /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
254+ /// ```
203255 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
204256 pub fn is_private ( & self ) -> bool {
205257 match ( self . octets ( ) [ 0 ] , self . octets ( ) [ 1 ] ) {
@@ -213,15 +265,25 @@ impl Ipv4Addr {
213265 /// Returns true if the address is link-local (169.254.0.0/16).
214266 ///
215267 /// This property is defined by [RFC 3927].
268+ ///
216269 /// [RFC 3927]: https://tools.ietf.org/html/rfc3927
270+ ///
271+ /// # Examples
272+ ///
273+ /// ```
274+ /// use std::net::Ipv4Addr;
275+ ///
276+ /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
277+ /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
278+ /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
279+ /// ```
217280 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
218281 pub fn is_link_local ( & self ) -> bool {
219282 self . octets ( ) [ 0 ] == 169 && self . octets ( ) [ 1 ] == 254
220283 }
221284
222285 /// Returns true if the address appears to be globally routable.
223286 /// See [iana-ipv4-special-registry][ipv4-sr].
224- /// [ipv4-sr]: http://goo.gl/RaZ7lg
225287 ///
226288 /// The following return false:
227289 ///
@@ -231,6 +293,24 @@ impl Ipv4Addr {
231293 /// - the broadcast address (255.255.255.255/32)
232294 /// - test addresses used for documentation (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24)
233295 /// - the unspecified address (0.0.0.0)
296+ ///
297+ /// [ipv4-sr]: http://goo.gl/RaZ7lg
298+ ///
299+ /// # Examples
300+ ///
301+ /// ```
302+ /// #![feature(ip)]
303+ ///
304+ /// use std::net::Ipv4Addr;
305+ ///
306+ /// fn main() {
307+ /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
308+ /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
309+ /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
310+ /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
311+ /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
312+ /// }
313+ /// ```
234314 pub fn is_global ( & self ) -> bool {
235315 !self . is_private ( ) && !self . is_loopback ( ) && !self . is_link_local ( ) &&
236316 !self . is_broadcast ( ) && !self . is_documentation ( ) && !self . is_unspecified ( )
@@ -240,7 +320,18 @@ impl Ipv4Addr {
240320 ///
241321 /// Multicast addresses have a most significant octet between 224 and 239,
242322 /// and is defined by [RFC 5771].
323+ ///
243324 /// [RFC 5771]: https://tools.ietf.org/html/rfc5771
325+ ///
326+ /// # Examples
327+ ///
328+ /// ```
329+ /// use std::net::Ipv4Addr;
330+ ///
331+ /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
332+ /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
333+ /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
334+ /// ```
244335 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
245336 pub fn is_multicast ( & self ) -> bool {
246337 self . octets ( ) [ 0 ] >= 224 && self . octets ( ) [ 0 ] <= 239
@@ -249,7 +340,17 @@ impl Ipv4Addr {
249340 /// Returns true if this is a broadcast address (255.255.255.255).
250341 ///
251342 /// A broadcast address has all octets set to 255 as defined in [RFC 919].
343+ ///
252344 /// [RFC 919]: https://tools.ietf.org/html/rfc919
345+ ///
346+ /// # Examples
347+ ///
348+ /// ```
349+ /// use std::net::Ipv4Addr;
350+ ///
351+ /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
352+ /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
353+ /// ```
253354 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
254355 pub fn is_broadcast ( & self ) -> bool {
255356 self . octets ( ) [ 0 ] == 255 && self . octets ( ) [ 1 ] == 255 &&
@@ -259,11 +360,23 @@ impl Ipv4Addr {
259360 /// Returns true if this address is in a range designated for documentation.
260361 ///
261362 /// This is defined in [RFC 5737]:
262- /// [RFC 5737]: https://tools.ietf.org/html/rfc5737
263363 ///
264364 /// - 192.0.2.0/24 (TEST-NET-1)
265365 /// - 198.51.100.0/24 (TEST-NET-2)
266366 /// - 203.0.113.0/24 (TEST-NET-3)
367+ ///
368+ /// [RFC 5737]: https://tools.ietf.org/html/rfc5737
369+ ///
370+ /// # Examples
371+ ///
372+ /// ```
373+ /// use std::net::Ipv4Addr;
374+ ///
375+ /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
376+ /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
377+ /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
378+ /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
379+ /// ```
267380 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
268381 pub fn is_documentation ( & self ) -> bool {
269382 match ( self . octets ( ) [ 0 ] , self . octets ( ) [ 1 ] , self . octets ( ) [ 2 ] , self . octets ( ) [ 3 ] ) {
@@ -277,6 +390,15 @@ impl Ipv4Addr {
277390 /// Converts this address to an IPv4-compatible IPv6 address.
278391 ///
279392 /// a.b.c.d becomes ::a.b.c.d
393+ ///
394+ /// # Examples
395+ ///
396+ /// ```
397+ /// use std::net::{Ipv4Addr, Ipv6Addr};
398+ ///
399+ /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
400+ /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 767));
401+ /// ```
280402 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
281403 pub fn to_ipv6_compatible ( & self ) -> Ipv6Addr {
282404 Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 ,
@@ -287,6 +409,15 @@ impl Ipv4Addr {
287409 /// Converts this address to an IPv4-mapped IPv6 address.
288410 ///
289411 /// a.b.c.d becomes ::ffff:a.b.c.d
412+ ///
413+ /// # Examples
414+ ///
415+ /// ```
416+ /// use std::net::{Ipv4Addr, Ipv6Addr};
417+ ///
418+ /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
419+ /// Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 49152, 767));
420+ /// ```
290421 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
291422 pub fn to_ipv6_mapped ( & self ) -> Ipv6Addr {
292423 Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0xffff ,
@@ -425,6 +556,7 @@ impl Ipv6Addr {
425556 /// Returns true for the special 'unspecified' address (::).
426557 ///
427558 /// This property is defined in [RFC 4291].
559+ ///
428560 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
429561 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
430562 pub fn is_unspecified ( & self ) -> bool {
@@ -434,6 +566,7 @@ impl Ipv6Addr {
434566 /// Returns true if this is a loopback address (::1).
435567 ///
436568 /// This property is defined in [RFC 4291].
569+ ///
437570 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
438571 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
439572 pub fn is_loopback ( & self ) -> bool {
@@ -458,6 +591,7 @@ impl Ipv6Addr {
458591 /// Returns true if this is a unique local address (fc00::/7).
459592 ///
460593 /// This property is defined in [RFC 4193].
594+ ///
461595 /// [RFC 4193]: https://tools.ietf.org/html/rfc4193
462596 pub fn is_unique_local ( & self ) -> bool {
463597 ( self . segments ( ) [ 0 ] & 0xfe00 ) == 0xfc00
@@ -466,6 +600,7 @@ impl Ipv6Addr {
466600 /// Returns true if the address is unicast and link-local (fe80::/10).
467601 ///
468602 /// This property is defined in [RFC 4291].
603+ ///
469604 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
470605 pub fn is_unicast_link_local ( & self ) -> bool {
471606 ( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfe80
@@ -481,6 +616,7 @@ impl Ipv6Addr {
481616 /// (2001:db8::/32).
482617 ///
483618 /// This property is defined in [RFC 3849].
619+ ///
484620 /// [RFC 3849]: https://tools.ietf.org/html/rfc3849
485621 pub fn is_documentation ( & self ) -> bool {
486622 ( self . segments ( ) [ 0 ] == 0x2001 ) && ( self . segments ( ) [ 1 ] == 0xdb8 )
@@ -524,6 +660,7 @@ impl Ipv6Addr {
524660 /// Returns true if this is a multicast address (ff00::/8).
525661 ///
526662 /// This property is defined by [RFC 4291].
663+ ///
527664 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
528665 #[ stable( since = "1.7.0" , feature = "ip_17" ) ]
529666 pub fn is_multicast ( & self ) -> bool {
0 commit comments