from_str been renamed for parse and is now a method that can be called on strings:
use std::net::SocketAddr; fn main() { let server_details = "127.0.0.1:80"; let server: SocketAddr = server_details .parse() .expect("Unable to parse socket address"); println!("{:?}", server); }
If you want to allow DNS records by IPv {4,6} addresses, you can use ToSocketAddrs :
use std::net::{TcpStream, ToSocketAddrs}; fn main() { let server_details = "stackoverflow.com:80"; let server: Vec<_> = server_details .to_socket_addrs() .expect("Unable to resolve domain") .collect(); println!("{:?}", server);
to_socket_addrs returns an iterator, since one DNS record can expand to several IP addresses! Please note that this code will not work on the playground, as network access is disabled there; you will need to try this on the spot.
source share