How can I turn Vec<String> into &[&str]?

How can I turn Vec into &[&str]?

By creating a new vector.

let vec = vec![String::from("abc")];
let slices: Vec<&str> = vec.iter().map(|s| s.as_str()).collect();

You can now borrow the new vector into a &[&str]. The string slices in the new vector point into the old vector.

Why do you want to do this? This sounds like a xy problem.

@RustyYato It's almost certainly related to this other thread, and the api the postgres crate provides.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.