[][src]Function weighted_levenshtein::distance

pub fn distance<T, U, V>(a: U, b: V) -> usize where
    T: PartialEq + EditWeight,
    U: AsRef<[T]>,
    V: AsRef<[T]>, 

Compute the Levenshtein distance between two sequences.

Examples:

  • Compute edit distance between strings, assuming ASCII or Latin-1.
assert_eq!(distance("abc", "aaxcc"), 3);
  • Compute edit distance between strings, respecting Unicode.
use unicode_segmentation::UnicodeSegmentation;
assert_eq!(
     distance(
        "🇬🇧 🇧🇬".graphemes(true).collect::<Vec<&str>>(),
        "🇬🇧 🏴󠁧󠁢󠁳󠁣󠁴󠁿".graphemes(true).collect::<Vec<&str>>()),
     1);
  • Compute a distance in words between two strings:
assert_eq!(
   distance(
      "The quick brown fox".split (' ').collect::<Vec<&str>>(),
      "The very quick brown cat".split (' ').collect::<Vec<&str>>()),
   2);
  • Compute a distance between two sequences of a user type:

assert_eq!(distance(vec![1, 2, 3], vec![0, 1, 3, 3, 4]), 3);