Help optimize this code? ethereum vanity address gen (easy)

You can eliminate the str::from_utf8 call by changing to_hex to return &[u8]:

fn to_hex<'a>(bytes: &[u8], buf: &'a mut [u8]) -> &'a [u8] {
    for i in 0..bytes.len() {
        buf[i * 2] = HEX_CHARS_LOWER[(bytes[i] >> 4) as usize];
        buf[i * 2 + 1] = HEX_CHARS_LOWER[(bytes[i] & 0xf) as usize];
    }
    &buf[0..bytes.len() * 2]
}

fn is_addr_matching(prefix: &str, suffix: &str, addr: &[u8]) -> bool {
    addr.starts_with(prefix.as_bytes()) && addr.ends_with(suffix.as_bytes())
}

Or you could eliminate the to_hex conversion completely, and instead convert prefix and suffix from hex to binary before starting your search. Note that if the prefix or suffix has an odd number of hex digits, you’ll need to mask the address before comparing it.