Table of content

What is this?

The rules of the game are explained in my original post. Also, I just found a cool new Repository for learning Rustlings

"The task is simple. Most exercises contain an error that keeps them from compiling, and it's up to you to fix it!"

New, task, do one rustling per day.

2nd Challenge

Challenge

I will try to make a challenge based on the chapter I’m reading in the rust book. I read the chapter on Strings so the challenge is to parse an arbitrary hex String 0124342 using only the String type and String slices.

Solution

Rust stores strings in UTF-8 encoding. UTF-8 is backward-compatible with ASCII which I’m accustomed to, and uses one byte u8 per ASCII character. This should make things simple. To convert an ASCII to a number we subtract 0x30 to convert a lower case letter to a number we subtract 0x57 and 0x37 for lower case.

	fn main() {
	    let hex: String = String::from("0123456789ABCDEFabcdef");

	    for b in hex.bytes(){
	        println!("ascii hex byte {} => integer {}", b, ascii_hex_to_u8(b));
	    }
	}

	fn ascii_hex_to_u8(c: u8) -> u8 {
	    match c {
	        c if c >= b'0' && c <= b'9' => c - b'0',
	        c if c >= b'a' && c <= b'f' => c - b'a' + 10,
	        c if c >= b'A' && c <= b'F' => c - b'A' + 10,
	        _ => panic!()
	    }
	}
	

Permalink running on Playground