Per Steve Klabnik's writeup in the pre-Rust 1.0 documentation on the difference between String
and &str
, in Rust you should use &str
unless you really need to have ownership over a String
. Similarly, it's recommended to use references to slices (&[]
) instead of Vec
s unless you really need ownership over the Vec
.
I have a Vec<String>
and I want to write a function that uses this sequence of strings and it doesn't need ownership over the Vec
or String
instances, should that function take &[&str]
? If so, what's the best way to reference the Vec<String>
into &[&str]
? Or, is this coercion overkill?
You can create a function that accepts both
&[String]
and&[&str]
using theAsRef
trait: