std-type

IpAddr: IPv4 or IPv6

Use an enum to accept both address families. Pattern matching allows family-specific processing without guessing from punctuation. Expected stdout (not executed): IPv4 IPv6 Common mistake: Using colon counting as a replacement for validated parsing.

Formal Syntax & Structure

rust syntaxrust
enum IpAddr { V4(Ipv4Addr), V6(Ipv6Addr) }

Working Example

Example Snippetrust
fn main() {    use std::net::IpAddr;    for text in ["192.0.2.3", "2001:db8::3"] {        match text.parse::<IpAddr>().unwrap() {            IpAddr::V4(_) => println!("IPv4"),            IpAddr::V6(_) => println!("IPv6"),        }    }}
Rust Reference