1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pub fn hamming(a: &str, b: &str) -> u32 {
assert_eq!(
a.len(),
b.len(),
"Hamming distance requires slices of equal length, lengths {} and {} given",
a.len(),
b.len()
);
let mut distance = 0;
let zipped = a.chars().zip(b.chars());
for (aa, bb) in zipped {
if aa != bb {
distance += 1;
}
}
distance
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn test_panic_on_not_equal() {
hamming("abc", "ab");
}
#[test]
fn test_empty_string() {
assert_eq!(hamming("", ""), 0);
}
#[test]
fn test_basic() {
assert_eq!(hamming("abcdefg", "0bc1ef2"), 3);
}
}