fn test_host_set() {
let old_hosts_raw = vec!["host1", "host2", "host3"];
let mut old_hosts = HostSet::new();
old_hosts.update(&old_hosts_raw);
// some use of old_hosts_raw
}
which will not work since the type of old_hosts_raw is Vec<&str>, and in HostSet.update, i in the map has type &&str rather than &str. Removing & in the &old_hosts_raw will cause any further use of old_hosts_raw illegal. I am wondering:
There should be some custom of writing such kind of code, just like in C++
std::transform(std::begin(other), std::end(other), std::back_inserter(container), |i| -> auto { return Container::value_t(i); });
As the error points out, IntoIterator::Item is &&str, not &str which <std::string::String as From<&str>> requires.
To have a Iterator that yields &str from one that yields &&str, use .copied() or .cloned() or .map(|s| *s).
error[E0277]: the trait bound `std::string::String: From<&&str>` is not satisfied
--> src/lib.rs:39:22
|
39 | old_hosts.update(old_hosts_raw.iter());
| ------ ^^^^^^^^^^^^^^^^^^^^ the trait `From<&&str>` is not implemented for `std::string::String`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `From<T>`:
<std::string::String as From<char>>
<std::string::String as From<Box<str>>>
<std::string::String as From<Cow<'a, str>>>
<std::string::String as From<&str>>
<std::string::String as From<&mut str>>
<std::string::String as From<&std::string::String>>
note: the method call chain might not have had the expected associated types
--> src/lib.rs:39:36
|
37 | let old_hosts_raw = vec!["host1", "host2", "host3"];
| ------------------------------- this expression has type `Vec<&str>`
38 | let mut old_hosts = HostSet::new();
39 | old_hosts.update(old_hosts_raw.iter());
| ^^^^^^ `IntoIterator::Item` is `&&str` here
note: required by a bound in `HostSet::update`
--> src/lib.rs:28:17
|
25 | pub fn update<T>(&mut self, other: T) -> &mut Self
| ------ required by a bound in this associated function
...
28 | String: From<<T as IntoIterator>::Item>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `HostSet::update`
Alternative solution
Change the signature of update
-String: From<<T as IntoIterator>::Item>,
+<T as IntoIterator>::Item: AsRef<str>,