我有这个工作代码:
/* 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...
是否有一种惯用的方法可以从枚举中获取所需的变体,而无需via
被提及三次?
或者这就是我应该忽略 Rust 中不需要的变体的方式?
您可以使用
let
-else
,因为else
分支是发散的 (continue
):