Проверьте, присутствует ли строка в тексте, используя Rust

Чтобы проверить наличие строки в тексте с помощью Rust, вы можете использовать несколько методов. Вот несколько примеров кода:

Метод 1: использование метода contains

fn main() {
    let text = "This is a sample text";
    let keyword = "sample";
    if text.contains(keyword) {
        println!("The keyword is present in the text");
    } else {
        println!("The keyword is not present in the text");
    }
}

Метод 2. Использование регулярных выражений с контейнером regex

use regex::Regex;
fn main() {
    let text = "This is a sample text";
    let keyword = "sample";
    let re = Regex::new(keyword).unwrap();
    if re.is_match(text) {
        println!("The keyword is present in the text");
    } else {
        println!("The keyword is not present in the text");
    }
}

Метод 3. Использование метода find

fn main() {
    let text = "This is a sample text";
    let keyword = "sample";
    if let Some(_) = text.find(keyword) {
        println!("The keyword is present in the text");
    } else {
        println!("The keyword is not present in the text");
    }
}

Метод 4. Использование метода rfindдля поиска последнего вхождения

fn main() {
    let text = "This is a sample text with sample keyword";
    let keyword = "sample";
    if let Some(_) = text.rfind(keyword) {
        println!("The keyword is present in the text");
    } else {
        println!("The keyword is not present in the text");
    }
}

Это всего лишь несколько примеров того, как можно проверить наличие строки в тексте с помощью Rust. Наиболее подходящий метод зависит от вашего конкретного случая использования.