Is there optimization for matching str

I am interested in that, when I use match my_str {..}, does Rust actually match all pattern in for-each mod or in internal hashing? Or anyway more effective? the same question for &[u8] and u64 matching. Thanks for everyones' patience

I don’t believe that the compiler is doing anything particularly intelligent to optimize this case beyond an equivalent if/else statement. For a more efficient solution, you’ll want to use something like the phf crate.

4 Likes

You may want to ask for details on the internals forum. It has been discussed there before.

1 Like

From my experience I've seen LLVM often optimize match on string patterns to something like a decision tree, first matching on the length and then matching usually 4 or 8 bytes at a time.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.