-2

A function I want to use requires a Vec<String> as parameter input.

What I have instead is either a string slice (&str) or a String.

My attempt:

let options_vec: Vec<String> = options.split(char::is_withespace).collect::<Vec<_>>();

The error I'm getting is:

value of type `std::vec::Vec<std::string::String>` cannot be built from `std::iter::Iterator<Item=&str>
Folaht
  • 927
  • 1
  • 10
  • 31

1 Answers1

3

split returns impl Iterator<Item = &str>, you need explicitly convert its items to String, for example like this:

let options_vec: Vec<String> = options
    .split(char::is_whitespace)
    .map(ToString::to_string)
    .collect::<Vec<_>>();
Masklinn
  • 23,560
  • 2
  • 21
  • 39
Kitsu
  • 2,646
  • 9
  • 23