How to find a substring in a string

how to find indexes of all matches with any string from the array in a string,

I tried to do it with [match_indices()]:

  for i in &keys {



but I would like to find all the matches in one iteration for optimization,

is there a way to do something like this?:


str::match_indices returns an iterator for a single pattern. If you have multiple patterns, you won't be able to optimize it beyond quadratic time complexity. The best you can do is use iterator adapters to sugar over the syntax. It won't be any faster. Rust Playground

You can write your own parser with something like nom that will be faster than calling match_indices multiple times

1 Like

This is exactly what the aho-corasick crate does. The regex crate even uses it in certain cases.


thank you,that's exactly what I need