Rust Playground (rust-lang.org)
In this example, the function has a signature of:
fn foo<'a, S>(iter: impl IntoIterator<Item = &'a (S, S)>) -> Vec<(&'a str, &'a str)>
where
S: AsRef<str> + 'a,
This seems to work in cases where the function is called as so:
let v_str = vec![("A", "1"), ("B", "2"), ("C", "3"), ("D", "4")];
let v_string: Vec<(String, String)> = v_str
.iter()
.map(|(s1, s2)| (s1.to_string(), s2.to_string()))
.collect();
dbg!(foo(v_str.iter()));
dbg!(foo(v_string.iter()));
but fails without the extra .iter()
:
error[E0271]: type mismatch resolving `<Vec<(&str, &str)> as IntoIterator>::Item == &(_, _)`
--> src/main.rs:8:14
|
8 | dbg!(foo(v_str));
| --- ^^^^^ expected `&(_, _)`, found `(&str, &str)`
| |
| required by a bound introduced by this call
|
= note: expected reference `&(_, _)`
found tuple `(&str, &str)`
Is there something I can do here to avoid needing the extra .iter()
in the function call? It looks like without it, the iter
will return (S, S)
instead of &(S, S)
.