Skip to content

Instantly share code, notes, and snippets.

@trimoq
Last active October 14, 2019 13:43
Show Gist options
  • Save trimoq/6c1cbfd1d1d0862d5af8cc396163e0c8 to your computer and use it in GitHub Desktop.
Save trimoq/6c1cbfd1d1d0862d5af8cc396163e0c8 to your computer and use it in GitHub Desktop.
Calculate the length of the common prefix of two slices
/*
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