# Is it a bug? a ipv4 IpAddr to SocketAddr

**URL:** <https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781>\
**Category:** uncategorized\
**Created:** [June 17, 2026, 11:23am UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781 "2026-06-17T11:23:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![goldli](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/goldli/32/52956_2.png) [@goldli](https://users.rust-lang.org/u/goldli)\
**Post date:** [June 17, 2026, 11:23am UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/1 "2026-06-17T11:23:53Z")

</div>

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](https://mega.nz/file/QrtTmS6a#vV8wd9XyXWDfj8H69YIvkPr1GiOOELPZ1emH1jGdrig)

sample code is

```rust
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**

```rust
    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

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [June 17, 2026, 11:41am UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/2 "2026-06-17T11:41:22Z")

</div>

if you look at the code of `SocketAddr::new` that you have shared , you can see that it behaves exactly as anyone would excpect and cannot convert ipv4 to ipv6.

you share some sample code, but `local_ip` is not an `std` function. you need to share what it is or where you got it from in order for us to be able to help you.

edit : i originally made a disparaing comment against the use of LLMs but it has become clear the one used here was only for translation

---

<div class="post-metadata">

**Author:** ![goldli](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/goldli/32/52956_2.png) [@goldli](https://users.rust-lang.org/u/goldli)\
**Post date:** [June 17, 2026, 11:50am UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/3 "2026-06-17T11:50:23Z")

</div>

the code is my test code. not copy from any web page. my question is :  
after call **local\_ip()** by crate **local-ip-address** , got a **IpAddr type** variable **local\_ip\_address** , it's ipv4, but when use **SocketAddr::new()** , the inner code  
**match ip {**  
**}**  
it will match **IpAddr::V6** , but not V4.  
you can see the video.

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [June 17, 2026, 12:01pm UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/4 "2026-06-17T12:01:11Z")

</div>

i have seen the video, and it certainly looks like a serious bug. but the code of `SocketAddr::new` remains airtight.  
it does not modify the input, so it can't change what variant this is.

i would advise you try to debug-print `ip` just before calling `SocketAddr::new`, and debug-print the SocketAddr right after, as i would find this more reliable than the debugger.

`SocketAddr` is an enum, so you could also rewrite `SocketAddr::new` by hand and add more debugging there

---

<div class="post-metadata">

**Author:** ![yvt](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yvt/32/48843_2.png) [@yvt](https://users.rust-lang.org/u/yvt)\
**Post date:** [June 17, 2026, 12:11pm UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/5 "2026-06-17T12:11:17Z")

</div>

I'd take what the debugger says with a grain of salt in general. Debug information generation is one of the most under-tested and difficult areas in a compiler. In your video it says `scope_id = 32759`, which is impossible in the given context, suggesting the debugger is most likely showing incorrect information.

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [June 17, 2026, 12:16pm UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/6 "2026-06-17T12:16:05Z")

</div>

> [@yvt](#):
>
> suggesting the debugger is most likely showing incorrect information.

that is most likely what is happening here, which is why i would recommend doing some print-debugging

---

<div class="post-metadata">

**Author:** ![goldli](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/goldli/32/52956_2.png) [@goldli](https://users.rust-lang.org/u/goldli)\
**Post date:** [June 17, 2026, 12:27pm UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/7 "2026-06-17T12:27:34Z")

</div>

i have tried print, before and after SocketAddr::new, both IpV4.

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [June 17, 2026, 12:37pm UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/8 "2026-06-17T12:37:05Z")

</div>

so it seems your debugger is the one with bugs. that is very unfortnate, and i have no clue what could cause it.

i would advise that you try to rely on debug printing whenever there is doubt from now on

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 15, 2026, 12:37pm UTC](https://users.rust-lang.org/t/is-it-a-bug-a-ipv4-ipaddr-to-socketaddr/140781/9 "2026-09-15T12:37:26Z")

</div>

Closed on September 15, 2026, 12:37pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
