As per this answer, consider a function which accepts a Vec
/slice of String
/&str
:
fn foo<S: AsRef<str>>(list: &[S]) {
for s in list.iter() {
println!("{}", s.as_ref());
}
}
fn main() {
foo(&["abc", "def"]);
foo(&["abc", &format!("def {}", 99)]);
foo(&[&format!("def {}", 99), "abc"]); // error
}
In the last call, I receive the following error:
error[E0308]: mismatched types
--> src/main.rs:10:9
|
10 | foo(&[&format!("def {}", 99), "abc"]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice, found array `[&str; 2]`
|
= note: expected reference `&[&String]`
found reference `&[&str; 2]`
In order to have format!
as the first element, I can't use references, but owned values instead:
foo(&[format!("def {}", 99), "abc".to_owned()]);
Why?