@@ -154,6 +154,14 @@ impl Ipv4Addr {
154
154
/// Creates a new IPv4 address from four eight-bit octets.
155
155
///
156
156
/// 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
+ /// ```
157
165
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
158
166
pub fn new ( a : u8 , b : u8 , c : u8 , d : u8 ) -> Ipv4Addr {
159
167
Ipv4Addr {
@@ -167,6 +175,15 @@ impl Ipv4Addr {
167
175
}
168
176
169
177
/// 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
+ /// ```
170
187
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
171
188
pub fn octets ( & self ) -> [ u8 ; 4 ] {
172
189
let bits = ntoh ( self . inner . s_addr ) ;
@@ -176,8 +193,18 @@ impl Ipv4Addr {
176
193
/// Returns true for the special 'unspecified' address (0.0.0.0).
177
194
///
178
195
/// 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
+ /// ```
181
208
#[ stable( feature = "ip_shared" , since = "1.12.0" ) ]
182
209
pub fn is_unspecified ( & self ) -> bool {
183
210
self . inner . s_addr == 0
@@ -186,7 +213,17 @@ impl Ipv4Addr {
186
213
/// Returns true if this is a loopback address (127.0.0.0/8).
187
214
///
188
215
/// This property is defined by [RFC 1122].
216
+ ///
189
217
/// [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
+ /// ```
190
227
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
191
228
pub fn is_loopback ( & self ) -> bool {
192
229
self . octets ( ) [ 0 ] == 127
@@ -195,11 +232,26 @@ impl Ipv4Addr {
195
232
/// Returns true if this is a private address.
196
233
///
197
234
/// The private address ranges are defined in [RFC 1918] and include:
198
- /// [RFC 1918]: https://tools.ietf.org/html/rfc1918
199
235
///
200
236
/// - 10.0.0.0/8
201
237
/// - 172.16.0.0/12
202
238
/// - 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
+ /// ```
203
255
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
204
256
pub fn is_private ( & self ) -> bool {
205
257
match ( self . octets ( ) [ 0 ] , self . octets ( ) [ 1 ] ) {
@@ -213,15 +265,25 @@ impl Ipv4Addr {
213
265
/// Returns true if the address is link-local (169.254.0.0/16).
214
266
///
215
267
/// This property is defined by [RFC 3927].
268
+ ///
216
269
/// [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
+ /// ```
217
280
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
218
281
pub fn is_link_local ( & self ) -> bool {
219
282
self . octets ( ) [ 0 ] == 169 && self . octets ( ) [ 1 ] == 254
220
283
}
221
284
222
285
/// Returns true if the address appears to be globally routable.
223
286
/// See [iana-ipv4-special-registry][ipv4-sr].
224
- /// [ipv4-sr]: http://goo.gl/RaZ7lg
225
287
///
226
288
/// The following return false:
227
289
///
@@ -231,6 +293,24 @@ impl Ipv4Addr {
231
293
/// - the broadcast address (255.255.255.255/32)
232
294
/// - test addresses used for documentation (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24)
233
295
/// - 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
+ /// ```
234
314
pub fn is_global ( & self ) -> bool {
235
315
!self . is_private ( ) && !self . is_loopback ( ) && !self . is_link_local ( ) &&
236
316
!self . is_broadcast ( ) && !self . is_documentation ( ) && !self . is_unspecified ( )
@@ -240,7 +320,18 @@ impl Ipv4Addr {
240
320
///
241
321
/// Multicast addresses have a most significant octet between 224 and 239,
242
322
/// and is defined by [RFC 5771].
323
+ ///
243
324
/// [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
+ /// ```
244
335
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
245
336
pub fn is_multicast ( & self ) -> bool {
246
337
self . octets ( ) [ 0 ] >= 224 && self . octets ( ) [ 0 ] <= 239
@@ -249,7 +340,17 @@ impl Ipv4Addr {
249
340
/// Returns true if this is a broadcast address (255.255.255.255).
250
341
///
251
342
/// A broadcast address has all octets set to 255 as defined in [RFC 919].
343
+ ///
252
344
/// [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
+ /// ```
253
354
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
254
355
pub fn is_broadcast ( & self ) -> bool {
255
356
self . octets ( ) [ 0 ] == 255 && self . octets ( ) [ 1 ] == 255 &&
@@ -259,11 +360,23 @@ impl Ipv4Addr {
259
360
/// Returns true if this address is in a range designated for documentation.
260
361
///
261
362
/// This is defined in [RFC 5737]:
262
- /// [RFC 5737]: https://tools.ietf.org/html/rfc5737
263
363
///
264
364
/// - 192.0.2.0/24 (TEST-NET-1)
265
365
/// - 198.51.100.0/24 (TEST-NET-2)
266
366
/// - 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
+ /// ```
267
380
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
268
381
pub fn is_documentation ( & self ) -> bool {
269
382
match ( self . octets ( ) [ 0 ] , self . octets ( ) [ 1 ] , self . octets ( ) [ 2 ] , self . octets ( ) [ 3 ] ) {
@@ -277,6 +390,15 @@ impl Ipv4Addr {
277
390
/// Converts this address to an IPv4-compatible IPv6 address.
278
391
///
279
392
/// 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
+ /// ```
280
402
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
281
403
pub fn to_ipv6_compatible ( & self ) -> Ipv6Addr {
282
404
Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 ,
@@ -287,6 +409,15 @@ impl Ipv4Addr {
287
409
/// Converts this address to an IPv4-mapped IPv6 address.
288
410
///
289
411
/// 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
+ /// ```
290
421
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
291
422
pub fn to_ipv6_mapped ( & self ) -> Ipv6Addr {
292
423
Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0xffff ,
@@ -425,6 +556,7 @@ impl Ipv6Addr {
425
556
/// Returns true for the special 'unspecified' address (::).
426
557
///
427
558
/// This property is defined in [RFC 4291].
559
+ ///
428
560
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
429
561
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
430
562
pub fn is_unspecified ( & self ) -> bool {
@@ -434,6 +566,7 @@ impl Ipv6Addr {
434
566
/// Returns true if this is a loopback address (::1).
435
567
///
436
568
/// This property is defined in [RFC 4291].
569
+ ///
437
570
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
438
571
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
439
572
pub fn is_loopback ( & self ) -> bool {
@@ -458,6 +591,7 @@ impl Ipv6Addr {
458
591
/// Returns true if this is a unique local address (fc00::/7).
459
592
///
460
593
/// This property is defined in [RFC 4193].
594
+ ///
461
595
/// [RFC 4193]: https://tools.ietf.org/html/rfc4193
462
596
pub fn is_unique_local ( & self ) -> bool {
463
597
( self . segments ( ) [ 0 ] & 0xfe00 ) == 0xfc00
@@ -466,6 +600,7 @@ impl Ipv6Addr {
466
600
/// Returns true if the address is unicast and link-local (fe80::/10).
467
601
///
468
602
/// This property is defined in [RFC 4291].
603
+ ///
469
604
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
470
605
pub fn is_unicast_link_local ( & self ) -> bool {
471
606
( self . segments ( ) [ 0 ] & 0xffc0 ) == 0xfe80
@@ -481,6 +616,7 @@ impl Ipv6Addr {
481
616
/// (2001:db8::/32).
482
617
///
483
618
/// This property is defined in [RFC 3849].
619
+ ///
484
620
/// [RFC 3849]: https://tools.ietf.org/html/rfc3849
485
621
pub fn is_documentation ( & self ) -> bool {
486
622
( self . segments ( ) [ 0 ] == 0x2001 ) && ( self . segments ( ) [ 1 ] == 0xdb8 )
@@ -524,6 +660,7 @@ impl Ipv6Addr {
524
660
/// Returns true if this is a multicast address (ff00::/8).
525
661
///
526
662
/// This property is defined by [RFC 4291].
663
+ ///
527
664
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
528
665
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
529
666
pub fn is_multicast ( & self ) -> bool {
0 commit comments