I have this working code:
/* Ignore IPv6. */
let via = if let IpAddr::V4(via) = addr.ip() {
via
} else {
warn!("(unsupported) ipv6 packet from ${addr}");
continue // abandon this packet and try the next packet
};
// ...process this packet with "via: Ipv4Addr" in scope...
Is there an idiomatic way to get the wanted variant out of an enum, without via
being mentioned three times?
Or is this how I should ignore unwanted variants in Rust?
You can use let
-else
, since the else
branch is diverging (continue
):
/* Ignore IPv6. */
let IpAddr::V4(via) = addr.ip() else {
warn!("(unsupported) ipv6 packet from ${addr}");
continue // abandon this packet and try the next packet
};
// ...process this packet with "via: Ipv4Addr" in scope...