Skip to content

Commit 336e5dd

Browse files
Add missing examples for IpAddr enum
1 parent f50dbd5 commit 336e5dd

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/libstd/net/ip.rs

+88
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,18 @@ pub enum Ipv6MulticastScope {
7979

8080
impl IpAddr {
8181
/// Returns true for the special 'unspecified' address ([IPv4], [IPv6]).
82+
///
8283
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
8384
/// [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+
/// ```
8494
#[stable(feature = "ip_shared", since = "1.12.0")]
8595
pub fn is_unspecified(&self) -> bool {
8696
match *self {
@@ -90,8 +100,18 @@ impl IpAddr {
90100
}
91101

92102
/// Returns true if this is a loopback address ([IPv4], [IPv6]).
103+
///
93104
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
94105
/// [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+
/// ```
95115
#[stable(feature = "ip_shared", since = "1.12.0")]
96116
pub fn is_loopback(&self) -> bool {
97117
match *self {
@@ -101,8 +121,23 @@ impl IpAddr {
101121
}
102122

103123
/// Returns true if the address appears to be globally routable ([IPv4], [IPv6]).
124+
///
104125
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
105126
/// [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+
/// ```
106141
pub fn is_global(&self) -> bool {
107142
match *self {
108143
IpAddr::V4(ref a) => a.is_global(),
@@ -111,8 +146,18 @@ impl IpAddr {
111146
}
112147

113148
/// Returns true if this is a multicast address ([IPv4], [IPv6]).
149+
///
114150
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
115151
/// [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+
/// ```
116161
#[stable(feature = "ip_shared", since = "1.12.0")]
117162
pub fn is_multicast(&self) -> bool {
118163
match *self {
@@ -122,8 +167,23 @@ impl IpAddr {
122167
}
123168

124169
/// Returns true if this address is in a range designated for documentation ([IPv4], [IPv6]).
170+
///
125171
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
126172
/// [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+
/// ```
127187
pub fn is_documentation(&self) -> bool {
128188
match *self {
129189
IpAddr::V4(ref a) => a.is_documentation(),
@@ -132,6 +192,20 @@ impl IpAddr {
132192
}
133193

134194
/// 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+
/// ```
135209
#[unstable(feature = "ipaddr_checker", issue = "36949")]
136210
pub fn is_ipv4(&self) -> bool {
137211
match *self {
@@ -141,6 +215,20 @@ impl IpAddr {
141215
}
142216

143217
/// 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+
/// ```
144232
#[unstable(feature = "ipaddr_checker", issue = "36949")]
145233
pub fn is_ipv6(&self) -> bool {
146234
match *self {

0 commit comments

Comments
 (0)