找不到str的匹配方法

问题描述 投票:0回答:1

我正在使用rayon对文本执行并行迭代,并尝试输出包含特定字符的字符串。我正在使用matches()matches()

playground

但显示错误:

use rayon::prelude::*;

fn main() {
    let text =
        "Some are born great, some achieve greatness, and some have greatness thrust upon them."
            .to_string();
    check(text, 's');
}

fn check(text: String, ch: char) {
    let words: Vec<_> = text.split_whitespace().collect();
    let words_with_ch: Vec<_> = words.par_iter().map(|ch| words.matches(ch)).collect();
    println!(
        "The following words contain the letter {:?}: {:?}",
        ch, words_with_ch
    );
}

我该如何解决此编译错误?

string rust
1个回答
0
投票

如@zerkms所建议,filter()将是解决此问题的好函数。

过滤器在迭代器上工作。 split_whitespace()创建迭代器,然后可以将其传递给filter()。

error[E0599]: no method named `matches` found for type `std::vec::Vec<&str>` in the current scope
  --> src/main.rs:12:65
   |
12 |     let words_with_ch: Vec<_> = words.par_iter().map(|ch| words.matches(ch)).collect();
   |                                                                 ^^^^^^^
© www.soinside.com 2019 - 2024. All rights reserved.