IpAddr
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
// pub enum IPAddr {
// V4(Ipv4Addr),
// V6(Ipv6Addr)
// }
fn main() {
let v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
let v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
assert_eq!(Ok(v4), "127.0.0.1".parse());
assert_eq!(Ok(v6), "::1".parse());
assert!(v4.is_loopback());
assert!(v6.is_loopback());
assert!(!IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)).is_loopback());
assert_eq!(v4.is_multicast(), false);
assert_eq!(v6.is_multicast(), false);
assert!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast());
assert!(v4.is_ipv4());
assert!(v6.is_ipv6());
}
SocketAddr
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
fn main() {
let mut v4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
let v6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8000);
assert_eq!(v4.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
v4.set_ip(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)));
assert_eq!(v4.ip(), IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)));
assert_eq!(v4.port(), 8080);
v4.set_port(8031);
assert!(v4.is_ipv4());
assert!(v6.is_ipv6());
}