Dual-licensed under MIT or the UNLICENSE.
https://docs.rs/aho-corasick/.
The documentation contains several examples, and there is a more complete example as a full program in examples/dict-search.rs
.
Here is a quick example showing simple substring matching:
use aho_corasick::{Automaton, AcAutomaton, Match}; let aut = AcAutomaton::new(vec!["apple", "maple"]); let mut it = aut.find("I like maple apples."); assert_eq!(it.next(), Some(Match { pati: 1, start: 7, end: 12, })); assert_eq!(it.next(), Some(Match { pati: 0, start: 13, end: 18, })); assert_eq!(it.next(), None);
Aho-Corasick is useful for matching multiple substrings against many long strings. If your long string is fixed, then you might consider building a suffix array of the search text (which takes O(n)
time). Matches can then be found in O(plogn)
time.