Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Check If A Text Pattern Matches With Rust's nom Crate

Code

use nom::bytes::complete::tag_no_case;

fn main() {
    assert_eq!(false, matches("brown dog"));
    assert_eq!(true, matches("red fox"));
}

fn matches(source: &str) -> bool {
    let check = tag_no_case::<&str, &str, ()>("red")(source).ok();
    match check {
        None => false,
        Some(_) => true,
    }
}