Converting a binary string to hexadecimal using Rust Once again, I am reinventing the wheel here to understand the fundamentals of rust. In a previous question I requested feedback on my function that performed a hexadecimal to binary conversion. In this snippet, I am seeking some constructive feedback on converting from binary to hexadecimal.

fn main() {

    let hexadecimal_value = convert_to_hex_from_binary("10111");
    println!("Converted: {}", hexadecimal_value);
}

fn convert_to_hex_from_binary(binary: &amp;str) -&gt; String {
    let padding_count = 4 - binary.len() % 4;

    let padded_binary = if padding_count &gt; 0 {
        ["0".repeat(padding_count), binary.to_string()].concat()
    } else {
        binary.to_string()
    };

    let mut counter = 0;
    let mut hex_string = String::new();
    while counter &lt; padded_binary.len() {
        let converted = to_hex(&amp;padded_binary[counter..counter + 4]);
        hex_string.push_str(converted);
        counter += 4;
    }

    hex_string
}

fn to_hex(b: &amp;str) -&gt; &amp;str {
    match b {
        "0000" =&gt; "0",
        "0001" =&gt; "1",
        "0010" =&gt; "2",
        "0011" =&gt; "3",
        "0100" =&gt; "4",
        "0101" =&gt; "5",
        "0110" =&gt; "6",
        "0111" =&gt; "7",
        "1000" =&gt; "8",
        "1001" =&gt; "9",
        "1010" =&gt; "A",
        "1011" =&gt; "B",
        "1100" =&gt; "C",
        "1101" =&gt; "D",
        "1110" =&gt; "E",
        "1111" =&gt; "F",
        _ =&gt; "",
    }
}
A couple of things:


Any time you have a function which could fail, it should return an Option&lt;T&gt;.  Ask yourself, if someone calls convert_to_hex_from_binary("foobar") and gets back "", is that reasonable?  They will need to manually check that their input makes sense, or that the output makes sense every time.  Static checking of these errors is part of the joy of Rust. 


With that it mind, change to_hex like so:

fn to_hex(b: &amp;str) -&gt; Option&lt;&amp;str&gt; {
    match b {
        "0000" =&gt; Some("0"),
        "0001" =&gt; Some("1"),
        "0010" =&gt; Some("2"),
        "0011" =&gt; Some("3"),
        "0100" =&gt; Some("4"),
        "0101" =&gt; Some("5"),
        "0110" =&gt; Some("6"),
        "0111" =&gt; Some("7"),
        "1000" =&gt; Some("8"),
        "1001" =&gt; Some("9"),
        "1010" =&gt; Some("A"),
        "1011" =&gt; Some("B"),
        "1100" =&gt; Some("C"),
        "1101" =&gt; Some("D"),
        "1110" =&gt; Some("E"),
        "1111" =&gt; Some("F"),
        _ =&gt; None,
    }
}



You are iterating over a known range of values to get counter.  Instead of using a while loop, you can use a range.  Since you're now returning None from to_hex you can now shortcut returning if something is wrong with None.


Your inner loop now looks like this:

let mut hex_string = String::new();
for counter in (0..padded_binary.len()).step_by(4) {
    match to_hex(&amp;padded_binary[counter..counter + 4])
    {
        Some(converted) =&gt; hex_string.push_str(converted),
        None =&gt; return None
    };
}


Your function signature and return type also need to match Option&lt;String&gt;:

fn convert_to_hex_from_binary(binary: &amp;str) -&gt; Option&lt;String&gt; {
    ...
    Some(hex_string)
}



Your padded_binary definition can be simplified, since repeating "0" zero times is exactly the same as not repeating it.


The definition is simply:

let padded_binary = ["0".repeat(padding_count), binary.to_string()].concat();


A couple of non Rust specific things:


Consider writing some tests.
String representation of binary numbers often begin with '0b'.  For example 0b1101 == 13.  You might want to consider checking for this prefix on the input string and trimming it.
You may have already thought about this, but consider if you want to trim whitespace or leave it to the function caller.