I’ve heard that fn x(argument:&str) is recommended over fn x(argument:&String) because rust can coerce strings into &str
And that it can do the same for &Vec -> &[]
But when I try to combine them I get an error. I get that I’m trying to coerce String into &str (rather than &String into &str) which isn’t allowed, but what’s the recommended way of handling this in a way that maximizes genericity? Is there a way to tell rust I want to borrow what’s inside the vector, not own it? I looked at the std::vec::Vec documentation and I don’t see a .as_borrowed() method.
If Rust had function overloading I could just make a constructor that would borrow the innards and then pass it along, but it doesn’t. Is there a String trait I can use to do this with generics?
fn main() {
let vector: Vec<String> = vec!["a".to_string(),"b".to_string(),"c".to_string()];
test(&vector);
}
fn test(something:&[&str]){
println!("{:?}",something);
}
Playground link for the lazy: https://play.rust-lang.org/?gist=3d9679dee088a8e8e4889fb22a422f96&version=stable&mode=debug&edition=2015