Skip to content

Instantly share code, notes, and snippets.

@rikkix
Created March 27, 2021 14:45
Show Gist options
  • Save rikkix/5e16033834d727ae90dbdd622f3b8490 to your computer and use it in GitHub Desktop.
Save rikkix/5e16033834d727ae90dbdd622f3b8490 to your computer and use it in GitHub Desktop.
first attempt on Rust
enum IPAddr {
IPv4(u8, u8, u8, u8),
IPv6(String),
}
fn main() {
let ip1 = IPAddr::IPv4(127, 0, 0, 1);
let ip2 = IPAddr::IPv6(String::from("::1"));
print_ip(&ip1);
print_ip(&ip2);
}
fn print_ip(ip: &IPAddr) {
match ip {
IPAddr::IPv4(a, b, c, d) =>
println!("IPv4: {}.{}.{}.{}", a, b, c, d),
IPAddr::IPv6(ip) =>
println!("IPv6: {}", ip),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment