Use the constant_time_eq function from the crypto-common crate to compare byte slices in constant time, preventing timing attacks. This function returns true if the slices are equal and false otherwise, regardless of where they differ.
use constant_time_eq::constant_time_eq;
fn main() {
let a = b"secret";
let b = b"secret";
let c = b"public";
assert!(constant_time_eq(a, b));
assert!(!constant_time_eq(a, c));
}
Add constant_time_eq = "0.3" to your Cargo.toml dependencies.