-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
  • 1,053
  • 1
  • 13
  • 32

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
  • 34,759
  • 3
  • 38
  • 57
Kitsu
  • 3,166
  • 14
  • 28