When a string is stored as a String
rather than a &str
it fails to implement the trait ToSocketAddrs
. The closest possible one that does is impl<'a> ToSocketAddrs for (&'a str, u16)
.
use std::net::TcpStream;
fn main() {
let url = "www.google.com".to_string(); // String
let url2 = "www.google.com"; // &'static str
let port = 80;
// Does not work
let tcp = TcpStream::connect((url, port));
// Works
let tcp2 = TcpStream::connect((url2, port));
}
This fails with:
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 coerce a String
into a &str
for the purposes of implementing the ToSocketAddrs
trait? From the documentation for Rust 1.0, I thought that String
would automatically move to &str
.
Converting Values to Strings Values can be explicitly converted to strings by calling either String() or n. toString() . With the String() function, let's convert a Boolean value to a string by passing the value true into the parameters for String() .
One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.
You can convert a string to a number in Node. js using any of these three methods: Number() , parseInt() , or parseFloat() .
To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed.
hauleth's answer will work for you, but it's not quite the whole story. In particular, your String
isn't coercing to a &str
because auto deref coercion does not kick in when trait matching, as per RFC 0401. Therefore, using a plain &url
won't work in this case because auto deref won't be applied to it. Instead of getting an &str
, you'll just get a &String
, which doesn't have a matching impl for ToSocketAddrs
. However, you can explicitly cause deref to happen with the dereference operator. In particular, &*url
should work. (&url[..]
also works because [..]
is the syntax for "take a slice over everything", but it's a bit more verbose.)
As std::net::ToSocketAddrs
is implemented only on (&str, _)
you need to take a slice of of the string using the &url[..]
slice syntax:
let tcp = TcpStream::connect((&url[..], port));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With