Is it a bug? a ipv4 IpAddr to SocketAddr

⚓ Rust    📅 2026-06-17    👤 surdeus    👁️ 1      

surdeus

using crate local-ip-address to get local ipv4 address , when convert it to SocketAddr, the method new will treat it as ipv6 address. see the video bellow

video about IpAddr to SocketAddr

sample code is

fn test_ip() {
    match local_ip() {
        Ok(local_ip_address) => {
            let x = SocketAddr::new(local_ip_address, 53);
        }
        Err(_) => {}
    }
}

the problem code is SocketAddr::new

    pub const fn new(ip: IpAddr, port: u16) -> SocketAddr {
        match ip {
            IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
            IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
        }
    }

the match item is IpAddr::V6, but not IpAddr::V4

rust: 1.96
os: mac os 12.7.6
ide: rustover 2026.1.3

7 posts - 3 participants

Read full topic

🏷️ Rust_feed