Hi,
I'm using this crate https://crates.io/crates/oauth1 and want to pass a HashMap<String, String>
as params so I need to convert it to HashMap<&str, Cow<str>>
. However when I using iter().map()
I got the error below. How do I implement that FromIterator
trait as compiler said?
Thanks in advance.
use std::borrow::Cow;
use std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
map.insert("hello".to_string(), "1".to_string());
map.insert("world".to_string(), "2".to_string());
println!("{:?}", map);
let params: HashMap<&str, Cow<str>> = map
.iter()
.map(|(key, value)| (key, Cow::from(value)))
.collect();
println!("{:?}", params);
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: a value of type `std::collections::HashMap<&str, std::borrow::Cow<'_, str>>` cannot be built from an iterator over elements of type `(&std::string::String, std::borrow::Cow<'_, str>)`
--> src/main.rs:14:10
|
14 | .collect();
| ^^^^^^^ value of type `std::collections::HashMap<&str, std::borrow::Cow<'_, str>>` cannot be built from `std::iter::Iterator<Item=(&std::string::String, std::borrow::Cow<'_, str>)>`
|
= help: the trait `std::iter::FromIterator<(&std::string::String, std::borrow::Cow<'_, str>)>` is not implemented for `std::collections::HashMap<&str, std::borrow::Cow<'_, str>>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.