In Rust, how can I pass a vector of owned objects to a function that expects a vector of borrowed objects? Is my only option to create a new vector?
What is the best practice for the signature of a function in which I care about the type of the contained generic of a struct but don't care about if it is borrowed or not?
Example situation:
fn using_vec_of_borrows(borrows: &Vec<&String>) {
//...
}
fn main() {
let foo: Vec<String> = Vec::new();
using_vec_of_borrows(&foo);
}
How could I write using_vec_of_borrows()
so that it accepts a vector of borrowed strings or a vector of owned strings? Or if it was part of an external library, could I convert my vector of owned strings to a vector of borrowed strings without iterating over it?
Keep in mind here that String
is just used as an example type. It could be anything.