@@ -79,8 +79,18 @@ pub enum Ipv6MulticastScope {
79
79
80
80
impl IpAddr {
81
81
/// Returns true for the special 'unspecified' address ([IPv4], [IPv6]).
82
+ ///
82
83
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
83
84
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
85
+ ///
86
+ /// # Examples
87
+ ///
88
+ /// ```
89
+ /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
90
+ ///
91
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
92
+ /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
93
+ /// ```
84
94
#[ stable( feature = "ip_shared" , since = "1.12.0" ) ]
85
95
pub fn is_unspecified ( & self ) -> bool {
86
96
match * self {
@@ -90,8 +100,18 @@ impl IpAddr {
90
100
}
91
101
92
102
/// Returns true if this is a loopback address ([IPv4], [IPv6]).
103
+ ///
93
104
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
94
105
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
106
+ ///
107
+ /// # Examples
108
+ ///
109
+ /// ```
110
+ /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
111
+ ///
112
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
113
+ /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
114
+ /// ```
95
115
#[ stable( feature = "ip_shared" , since = "1.12.0" ) ]
96
116
pub fn is_loopback ( & self ) -> bool {
97
117
match * self {
@@ -101,8 +121,23 @@ impl IpAddr {
101
121
}
102
122
103
123
/// Returns true if the address appears to be globally routable ([IPv4], [IPv6]).
124
+ ///
104
125
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
105
126
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
127
+ ///
128
+ /// # Examples
129
+ ///
130
+ /// ```
131
+ /// #![feature(ip)]
132
+ ///
133
+ /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
134
+ ///
135
+ /// fn main() {
136
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
137
+ /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(),
138
+ /// true);
139
+ /// }
140
+ /// ```
106
141
pub fn is_global ( & self ) -> bool {
107
142
match * self {
108
143
IpAddr :: V4 ( ref a) => a. is_global ( ) ,
@@ -111,8 +146,18 @@ impl IpAddr {
111
146
}
112
147
113
148
/// Returns true if this is a multicast address ([IPv4], [IPv6]).
149
+ ///
114
150
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
115
151
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
152
+ ///
153
+ /// # Examples
154
+ ///
155
+ /// ```
156
+ /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
157
+ ///
158
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
159
+ /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
160
+ /// ```
116
161
#[ stable( feature = "ip_shared" , since = "1.12.0" ) ]
117
162
pub fn is_multicast ( & self ) -> bool {
118
163
match * self {
@@ -122,8 +167,23 @@ impl IpAddr {
122
167
}
123
168
124
169
/// Returns true if this address is in a range designated for documentation ([IPv4], [IPv6]).
170
+ ///
125
171
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
126
172
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
173
+ ///
174
+ /// # Examples
175
+ ///
176
+ /// ```
177
+ /// #![feature(ip)]
178
+ ///
179
+ /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
180
+ ///
181
+ /// fn main() {
182
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
183
+ /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0))
184
+ /// .is_documentation(), true);
185
+ /// }
186
+ /// ```
127
187
pub fn is_documentation ( & self ) -> bool {
128
188
match * self {
129
189
IpAddr :: V4 ( ref a) => a. is_documentation ( ) ,
@@ -132,6 +192,20 @@ impl IpAddr {
132
192
}
133
193
134
194
/// Returns true if this address is a valid IPv4 address, false if it's a valid IPv6 address.
195
+ ///
196
+ /// # Examples
197
+ ///
198
+ /// ```
199
+ /// #![feature(ipaddr_checker)]
200
+ ///
201
+ /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
202
+ ///
203
+ /// fn main() {
204
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
205
+ /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(),
206
+ /// false);
207
+ /// }
208
+ /// ```
135
209
#[ unstable( feature = "ipaddr_checker" , issue = "36949" ) ]
136
210
pub fn is_ipv4 ( & self ) -> bool {
137
211
match * self {
@@ -141,6 +215,20 @@ impl IpAddr {
141
215
}
142
216
143
217
/// Returns true if this address is a valid IPv6 address, false if it's a valid IPv4 address.
218
+ ///
219
+ /// # Examples
220
+ ///
221
+ /// ```
222
+ /// #![feature(ipaddr_checker)]
223
+ ///
224
+ /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
225
+ ///
226
+ /// fn main() {
227
+ /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
228
+ /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(),
229
+ /// true);
230
+ /// }
231
+ /// ```
144
232
#[ unstable( feature = "ipaddr_checker" , issue = "36949" ) ]
145
233
pub fn is_ipv6 ( & self ) -> bool {
146
234
match * self {
0 commit comments