метод соответствий str не найден - PullRequest
0 голосов
/ 25 октября 2019

Я использую rayon для выполнения параллельной итерации над текстом и пытаюсь вывести строки, содержащие определенный символ. Я использую matches(): игровая площадка

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
    );
}

, но отображается ошибка:

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();
   |                                                                 ^^^^^^^

Как я могу решить эту проблемуошибка компиляции?

1 Ответ

0 голосов
/ 25 октября 2019

Как предполагает @zerkms, filter () будет хорошей функцией для решения этой проблемы.

Фильтр работает на итераторе. split_whitespace () создает итератор, который затем может быть передан в filter ().

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_with_ch: Vec<_> = text
        .split_whitespace()
        .filter(|w| w.contains(ch))
        .collect();
    println!(
        "The following words contain the letter {:?}: {:?}",
        ch, words_with_ch
    );
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...