How do I convert a Vec to Vec<&str>?

前端 未结 6 1353
春和景丽
春和景丽 2020-11-27 17:28

I can convert Vec to Vec<&str> this way:

let mut items = Vec::<&str>::new();
for item in &ano         


        
6条回答
  •  [愿得一人]
    2020-11-27 17:56

    The other answers simply work. I just want to point out that if you are trying to convert the Vec into a Vec<&str> only to pass it to a function taking Vec<&str> as argument, consider revising the function signature as:

    fn my_func>(list: &[T]) { ... }
    

    instead of:

    fn my_func(list: &Vec<&str>) { ... }
    

    As pointed out by this question: Function taking both owned and non-owned string collections. In this way both vectors simply work without the need of conversions.

提交回复
热议问题