Last active
October 14, 2019 13:43
-
-
Save trimoq/6c1cbfd1d1d0862d5af8cc396163e0c8 to your computer and use it in GitHub Desktop.
Calculate the length of the common prefix of two slices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Calculats the length of the common pfix of two slices. | |
The slices a and b are zipped, leading to an iterator containing a tuple with a component of each of the input iterators | |
The iterators take care of handling different-sized slices (also slices of zero size) | |
*/ | |
fn common_prefix_len(a: &str, b: &str) -> usize { | |
a.chars().zip( | |
b.chars() | |
).take_while( | |
|(x, y)| x == y | |
).count() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment