When a string is saved as a String rather than &str , it does not implement the ToSocketAddrs . The closest that it does is impl<'a> ToSocketAddrs for (&'a str, u16) .
use std::net::TcpStream; fn main() { let url = "www.google.com".to_string();
This fails:
error[E0277]: the trait bound `(std::string::String, {integer}): std::net::ToSocketAddrs` is not satisfied --> src/main.rs:9:11 | 9 | let tcp = TcpStream::connect((url, port)); | ^^^^^^^^^^^^^^^^^^ the trait `std::net::ToSocketAddrs` is not implemented for `(std::string::String, {integer})` | = help: the following implementations were found: <(std::net::Ipv6Addr, u16) as std::net::ToSocketAddrs> <(std::net::Ipv4Addr, u16) as std::net::ToSocketAddrs> <(&'a str, u16) as std::net::ToSocketAddrs> <(std::net::IpAddr, u16) as std::net::ToSocketAddrs> = note: required by `std::net::TcpStream::connect`
How can I force String to &str to implement the ToSocketAddrs sign? From the documentation for Rust 1.0, I thought String automatically move to &str .
source share