Function distances::strings::hamming

source ·
pub fn hamming<U: UInt>(x: &str, y: &str) -> U
Expand description

Computes the Hamming distance between two strings.

The Hamming distance is defined as the number of positions at which the corresponding symbols are different. It is named after Richard Hamming, who introduced it in his fundamental paper on Hamming codes.

While the input strings are not required to be of the same length, the distance will only be computed up to the length of the shorter string.

§Arguments

  • x: The first string.
  • y: The second string.

§Examples

use distances::strings::hamming;

let x = "NAJIBEATSPEPPERS";
let y = "NAJIBPEPPERSEATS";

let distance: u16 = hamming(x, y);

assert_eq!(distance, 10);

let x = "TOMEATSWHATFOODEATS";
let y = "FOODEATSWHATTOMEATS";

let distance: u16 = hamming(x, y);

assert_eq!(distance, 13);

§References