小编rus*_*sty的帖子

如何在 Rust 中返回字符串向量

如何通过分割中间有空格的字符串来返回字符串向量

fn line_to_words(line: &str) -> Vec<String> {     
    line.split_whitespace().collect()
}
    
fn main() {
    println!( "{:?}", line_to_words( "string with spaces in between" ) );
}
Run Code Online (Sandbox Code Playgroud)

上面的代码返回这个错误

line.split_whitespace().collect()
  |                           ^^^^^^^ value of type `std::vec::Vec<std::string::String>` cannot be built from `std::iter::Iterator<Item=&str>`
  |
  = help: the trait `std::iter::FromIterator<&str>` is not implemented for `std::vec::Vec<std::string::String>`
Run Code Online (Sandbox Code Playgroud)

string vector rust

8
推荐指数
1
解决办法
9142
查看次数

标签 统计

rust ×1

string ×1

vector ×1