How to convert Vec<String> to Vec<&str> in Rust?
Hello everyone! I started learning Rust and encountered a type conversion problem. I have a `Vec` and I need to pass it to a function that expects `Vec<&str>`. I tried simply casting the types, but the compiler complains.
Here's my code:
```rust
fn process_strings(strings: Vec<&str>) {
for s in strings {
println!("{}", s);
}
}
fn main() {
let owned_strings: Vec = vec!["hello".to_string(), "world".to_string()];
// Trying to pass Vec to a function expecting Vec<&str>
process_strings(owned_strings); // Error!
}
```
The compiler gives a type error: `expected Vec<&str>, found Vec`. I understand that `String` and `&str` are different types, but how do I convert one vector to another? I tried using `.iter().collect`
No answers yet.
Log in to leave an answer