Hamming Distance Over Bytes

Count differing bits between equal-length byte slices, report bit similarity, and reject inputs of unequal length.

RustData Integrity

Code

rustsnippet
36 lines
fn hamming_distance(left: &[u8], right: &[u8]) -> Result<u32, String> {
    if left.len() != right.len() {
        return Err(format!(
            "inputs must match in length: {} vs {}",
            left.len(),
            right.len()
        ));
    }
    Ok(left
        .iter()
        .zip(right.iter())
        .map(|(a, b)| (a ^ b).count_ones())
        .sum())
}

fn hamming_distance_bits(left: u64, right: u64) -> u32 {
    (left ^ right).count_ones()
}

fn similarity(left: &[u8], right: &[u8]) -> Result<f64, String> {
    if left.is_empty() {
        return Err("inputs must not be empty".to_string());
    }
    let distance = hamming_distance(left, right)?;
    let total_bits = (left.len() * 8) as f64;
    Ok(1.0 - f64::from(distance) / total_bits)
}

fn main() {
    let left = b"karolin";
    let right = b"kathrin";
    println!("distance: {:?}", hamming_distance(left, right));
    println!("similarity: {:.4}", similarity(left, right).unwrap_or(0.0));
    println!("bits: {}", hamming_distance_bits(0b1011, 0b1001));
    println!("length mismatch: {}", hamming_distance(b"abc", b"ab").is_err());
}

How the snippet works

One way to read it: the functions hamming_distance, hamming_distance_bits, similarity and main, doing all the work.

Failure is encoded in the return type — Result leaves no room for an unhandled error. A match spells out every case; omit one and the code does not compile.

What to watch for

  • Every fallible call yields a Result you must unwrap or propagate; there is no implicit throw.

Using it

The primary signature is fn hamming_distance(left: &[u8], right: &[u8]) -> Result<u32, String>

Running the file prints labeled results such as distance:, similarity:, bits: — each operation’s effect is visible in the output.

The demo flow inside main is hamming_distance, similarity and hamming_distance_bits — run the file to see it end to end.

Key statements

return Err("inputs must not be empty".to_string());
return Err(format!(
"inputs must match in length: {} vs {}",
left.len(),
right.len()
));
.zip(right.iter())
.map(|(a, b)| (a ^ b).count_ones())
.sum())
}

Interactive Tools for This Topic

FAQ

What happens when a call fails?
Each fallible operation returns `Result`; nothing is silently discarded.
What does Hamming Distance Over Bytes print when run?
The script runs standalone; its 4 labeled outputs include `distance:`, `similarity:`, `bits:`.