Skip to content

Commit 2f1f43f

Browse files
committed
Use slice patterns to check IP octets
1 parent a77dfcc commit 2f1f43f

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/libstd/net/ip.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,11 @@ impl Ipv4Addr {
483483
/// ```
484484
#[stable(since = "1.7.0", feature = "ip_17")]
485485
pub fn is_private(&self) -> bool {
486-
match (self.octets()[0], self.octets()[1]) {
487-
(10, _) => true,
488-
(172, b) if b >= 16 && b <= 31 => true,
489-
(192, 168) => true,
490-
_ => false
486+
match self.octets() {
487+
[10, _..] => true,
488+
[172, b, _..] if b >= 16 && b <= 31 => true,
489+
[192, 168, _..] => true,
490+
_ => false,
491491
}
492492
}
493493

@@ -509,7 +509,10 @@ impl Ipv4Addr {
509509
/// ```
510510
#[stable(since = "1.7.0", feature = "ip_17")]
511511
pub fn is_link_local(&self) -> bool {
512-
self.octets()[0] == 169 && self.octets()[1] == 254
512+
match self.octets() {
513+
[169, 254, _..] => true,
514+
_ => false,
515+
}
513516
}
514517

515518
/// Returns [`true`] if the address appears to be globally routable.
@@ -612,11 +615,11 @@ impl Ipv4Addr {
612615
/// ```
613616
#[stable(since = "1.7.0", feature = "ip_17")]
614617
pub fn is_documentation(&self) -> bool {
615-
match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) {
616-
(192, 0, 2, _) => true,
617-
(198, 51, 100, _) => true,
618-
(203, 0, 113, _) => true,
619-
_ => false
618+
match self.octets() {
619+
[192, 0, 2, _] => true,
620+
[198, 51, 100, _] => true,
621+
[203, 0, 113, _] => true,
622+
_ => false,
620623
}
621624
}
622625

0 commit comments

Comments
 (0)