Expand description
The Rust Standard Library documentation suggests that, for an argument of type
ToSocketAddrs you should directly pass a string literal, which is parsed at runtime and
thus potentially causing a panic. To solve this, socket_addr! and socket_addr_dyn! macros
do the parsing at compile-time and when the address is invalid, cause a compile error.
§Example
use socket_addr_macros::socket_addr;
use std::io::Write;
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind(socket_addr!(127.0.0.1:8080)).unwrap();
while let Ok((mut conn, _)) = listener.accept() {
conn.write(b"hello").unwrap();
}
}Macros§
- socket_
addr - Parses an IPv4 or IPv6 address at compile-time and returns a
SocketAddr. - socket_
addr_ dyn - Parses an IPv4 or IPv6 address at compile-time and returns either a
SocketAddrV4or aSocketAddrV6depending on the input.