Function bio::alignment::distance::hamming [] [src]

pub fn hamming(alpha: TextSlice, beta: TextSlice) -> Result<u32, &'static str>

Compute the Hamming distance between two strings with hamming. If returns the Result<u32, &str> type with the first element corresponding to the distance between two strings (a number of mismatches) and the second one to the error message when two strings are not of equal sizes.

Example

use bio::alignment::distance::*;

let x = b"GTCTGCATGCG";
let y = b"TTTAGCTAGCG";
// GTCTGCATGCG
//  |  ||  |||
// TTTAGCTAGCG
match hamming(x, y) {
    Ok(s)  => println!("Score is: {}", s),  // Score is 5
    Err(e) => println!("Error: {}", e),     // No error in this example
}
assert!(hamming(x, y).is_ok());
assert_eq!(hamming(x, y).unwrap(), 5);

In case of the error:

use bio::alignment::distance::*;

let x = b"GACTATATCGA";
let y = b"TTTAGCTC";
match hamming(x, y) {
    Ok(s)  => println!("Score is: {}", s),  // No score because strings are of unequal sizes.
    Err(e) => println!("Error: {}", e),     // Will print "Error: hamming distance: strings are of unequal sizes!"
}
assert!(hamming(x, y).is_err());