Rust challenge 2/100 - Parsing hex
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.