Convert Vec into a slice of &str in Rust?
Using AsRef trait
you can create a function that accepts both &[String]
and &[&str]
Example
fn test<T: AsRef<str>>(inp: &[T]) {
for x in inp { print!("{} ", x.as_ref()) }
println!("");
}
fn main() {
let vref = vec!["Hello", "world!"];
let vown = vec!["May the Force".to_owned(), "be with you.".to_owned()];
test(&vref);
test(&vown);
}
Output
Hello world!
May the Force be with you.
May the Force be with you.