Challenge 33

Rust Challenge: Calculate Hamming Distance

Write a function hamming_distance that calculates the Hamming Distance between two DNA strands (strings of C, A, G, T).

The Hamming Distance is the number of positions where the characters differ.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Solution

Click to Show/Hide Solution
#![allow(unused)] fn main() { fn hamming_distance(dna1: &str, dna2: &str) -> Result<usize, &'static str> { if dna1.len() != dna2.len() { return Err("DNA strands must be of equal length"); } Ok(dna1.chars().zip(dna2.chars()).filter(|(a, b)| a != b).count()) } }