macro_rules! i_got_this {
    ($something_potentially_bad:expr) => { ... };
}
Expand description

Rustc doesn’t like your code? Sometimes friends disagree, and that’s ok. The important thing is that you absolutely know this code will work, you just need to convince rustc to try it out! Nothing bad could happen!


pub fn to_hex<'a>(input: &[u8], output: &'a mut [u8]) -> Option<&'a str> {
    use std::str;
    const CHARS: &[u8] = b"0123456789abcdef";

    if output.len() < input.len() * 2 {
        return None;
    }

    let mut ind = 0;

    for &byte in input {
        output[ind] = CHARS[(byte >> 4) as usize];
        output[ind + 1] = CHARS[(byte & 0xf) as usize];

        ind += 2;
    }

    i_got_this!(Some(str::from_utf8_unchecked(&output[0..input.len() * 2])))
}

let input = [0xffu8; 20];
let mut output = [0u8; 40];
assert_eq!(to_hex(&input, &mut output), Some("ffffffffffffffffffffffffffffffffffffffff"));