@@ -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 ) ;
@@ -178,7 +195,16 @@ impl Ipv4Addr {
178
195
/// This property is defined in _UNIX Network Programming, Second Edition_,
179
196
/// W. Richard Stevens, p. 891; see also [ip7].
180
197
///
181
- /// [ip7]: (http://man7.org/linux/man-pages/man7/ip.7.html)
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
+ /// ```
182
208
#[ stable( feature = "ip_shared" , since = "1.12.0" ) ]
183
209
pub fn is_unspecified ( & self ) -> bool {
184
210
self . inner . s_addr == 0
@@ -189,6 +215,15 @@ impl Ipv4Addr {
189
215
/// This property is defined by [RFC 1122].
190
216
///
191
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
+ /// ```
192
227
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
193
228
pub fn is_loopback ( & self ) -> bool {
194
229
self . octets ( ) [ 0 ] == 127
@@ -203,6 +238,20 @@ impl Ipv4Addr {
203
238
/// - 192.168.0.0/16
204
239
///
205
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
+ /// ```
206
255
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
207
256
pub fn is_private ( & self ) -> bool {
208
257
match ( self . octets ( ) [ 0 ] , self . octets ( ) [ 1 ] ) {
@@ -218,6 +267,16 @@ impl Ipv4Addr {
218
267
/// This property is defined by [RFC 3927].
219
268
///
220
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
+ /// ```
221
280
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
222
281
pub fn is_link_local ( & self ) -> bool {
223
282
self . octets ( ) [ 0 ] == 169 && self . octets ( ) [ 1 ] == 254
@@ -236,6 +295,22 @@ impl Ipv4Addr {
236
295
/// - the unspecified address (0.0.0.0)
237
296
///
238
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
+ /// ```
239
314
pub fn is_global ( & self ) -> bool {
240
315
!self . is_private ( ) && !self . is_loopback ( ) && !self . is_link_local ( ) &&
241
316
!self . is_broadcast ( ) && !self . is_documentation ( ) && !self . is_unspecified ( )
@@ -247,6 +322,16 @@ impl Ipv4Addr {
247
322
/// and is defined by [RFC 5771].
248
323
///
249
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
+ /// ```
250
335
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
251
336
pub fn is_multicast ( & self ) -> bool {
252
337
self . octets ( ) [ 0 ] >= 224 && self . octets ( ) [ 0 ] <= 239
@@ -257,6 +342,15 @@ impl Ipv4Addr {
257
342
/// A broadcast address has all octets set to 255 as defined in [RFC 919].
258
343
///
259
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
+ /// ```
260
354
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
261
355
pub fn is_broadcast ( & self ) -> bool {
262
356
self . octets ( ) [ 0 ] == 255 && self . octets ( ) [ 1 ] == 255 &&
@@ -272,6 +366,17 @@ impl Ipv4Addr {
272
366
/// - 203.0.113.0/24 (TEST-NET-3)
273
367
///
274
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
+ /// ```
275
380
#[ stable( since = "1.7.0" , feature = "ip_17" ) ]
276
381
pub fn is_documentation ( & self ) -> bool {
277
382
match ( self . octets ( ) [ 0 ] , self . octets ( ) [ 1 ] , self . octets ( ) [ 2 ] , self . octets ( ) [ 3 ] ) {
@@ -285,6 +390,15 @@ impl Ipv4Addr {
285
390
/// Converts this address to an IPv4-compatible IPv6 address.
286
391
///
287
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
+ /// ```
288
402
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
289
403
pub fn to_ipv6_compatible ( & self ) -> Ipv6Addr {
290
404
Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0 ,
@@ -295,6 +409,15 @@ impl Ipv4Addr {
295
409
/// Converts this address to an IPv4-mapped IPv6 address.
296
410
///
297
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
+ /// ```
298
421
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
299
422
pub fn to_ipv6_mapped ( & self ) -> Ipv6Addr {
300
423
Ipv6Addr :: new ( 0 , 0 , 0 , 0 , 0 , 0xffff ,
0 commit comments