I am playing with Iron
, and I ran into this problem.
fn main() {
let mut router = Router::new();
let address = "127.0.0.1"; // or maybe "::1/128"
let port = 3000;
let ip = std::net::IpAddr::new(address); // does not exist
Iron::new(router).http((ip, port)).unwrap();
}
The http()
method takes a struct that implements ToSocketAddrs
. (&str, u16)
implements this trait, but I prefer to verify the validity of user input before the http()
method is called.
I saw that (std::net::IpAddr, u16)
implements this trait, but I do not know how to build an IpAddr
“agnostically”: maybe the user wrote an IPv4 address, maybe an IPv6.
Is there a way to create an IpAddr
from a string only? I think that it is possible because I can give to it a (&str, u16)
.