Regarding iteration of a str's chars (instead of bytes), things like .enumerate() and .position() must be avoided if the usize is to be used to index the str. Indeed, the i-th char does not have to be at the i-th byte (except when i = 0 ), since the UTF-8 encoding of a Unicode char may not be 1-byte long. For such situations, the *_indices methods should be used (such as .match_indices()):
/// Return the index of the first occurence of a char matching
/// predicate, if any.
fn str_find (
s: &'_ str,
predicate: impl FnMut(char) -> bool,
) -> Option<usize>
{
// rmatch_indices for an rfind
s.match_indices(predicate).next().map(|(i, _)| i)
}
and in the same vein, .char_indices() ought to be used rather than .chars().enumerate()