About the std lib and std::net


I have a couple of questions:

  1. May I suggest to have a separate category in this forum just for "Rust std lib"? It basically gives a more focused place to ask questions about the std lib.

  2. Is there a place (e.g. blog, or other posts) where we can find out what is the plan for the std::net ? AFAIK, there have been multiple efforts (net2 crate, socket2 crate, related nix crate). to provide a better network/socket programming support, but nothing offers a complete solution and std::net seems to be stuck in the past. Is there any plan to merge some new features from crates back into std::net?


1 Like

They're not mean to be a better std. The stdlib provides portable interface across common platforms like linux and windows. For the portability it can generally expose features which is supported all the platforms it support. The socket2 crate is not for portability but a safe api for advanced configurations that people used to do with direct libc calls. The nix crate is similar but its scope is not just sockets but the whole unix-like system interface.

Quote from the socket2's root document:

This crate provides as direct as possible access to the system’s functionality for sockets, this means little effort to provide cross-platform utilities. It is up to the user to know how to use sockets when using this crate. If you don’t know how to create a socket using libc/system calls then this crate is not for you.

+Note that the net2 is deprecated. Use socket2 instead.

Let me give an example to show why I'm asking about this.

For UDP socket, often times we want to set it to reuse address (and port). Although there is details difference between platforms, the general API (setsockopt with SO_REUSEADDR) for such feature is more or less the same. In socket2, it is set_reuse_address(), which is a cross-platform API. Can't std::net provide it? Currently it does not.

The difference is big enough to affect simple program. Portability doesn't means the same code can compile across platforms but may behave differently. The socket2 crate explicitly mentioned that it's not for portability and it does what underlying system does.


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.