DEV Community

Abinash Sahoo
Abinash Sahoo

Posted on

For the first time ever Rust helps me with my personal problem!🦀

Today, I received an email from GitHub asking me to set up two-factor authentication (2FA) for my account.

During the setup, GitHub provided me with backup codes in case I ever needed to recover my account.

The platform suggested I write these codes down or print them out and delete the text file, as it’s not safe to store them on my computer.

When I looked at the backup codes, I noticed they were made up of 32 words and while writing it down I realised that these are hexadecimal numbers.

Suddenly, I had an idea: "Why not convert these hex numbers into decimals too and write them down?"

At first, I thought of using an online hexadecimal-to-decimal converter, but I realized it would take at least 30 minutes to convert all 32 codes manually.

Then it hit me: "I know Rust! Why not write a small program to do this for me?"

So, I opened my editor and wrote a simple Rust program that reads the backup codes from a text file, processes them, and converts them from hexadecimal to decimal.

Here's the code I came up with:

use std::fs;

const PATH: &str = "test.txt";

fn main() {
    let text: String = fs::read_to_string(PATH).unwrap();

    let mut code_vec_str: Vec<&str> = Vec::new();

    text.lines()
        .for_each(|line| {
            line.split('-')
                .for_each(|code| code_vec.push(code))
        });

    let mut num_code: Vec<u32> = Vec::new();

    for code in code_vec {
        num_code.push(u32::from_str_radix(code, 16).unwrap());
    }

    println!("{:?}", num_code);
}
Enter fullscreen mode Exit fullscreen mode

And just like that, in no time, I had my list of decimal numbers!

It was such a small program, but it saved me time and effort.

For me, this was a big achievement because it was the first time I used programming to solve a real-life problem.

This experience reminded me why learning a programming language like Rust is so valuable.

Even the smallest piece of code can make a big difference and solve real problems.

So, whether it's Rust or another language, start learning, and soon enough, you’ll find yourself using it in ways you never expected!

I hope this gives you a little motivation to keep your programming journey going.

Have a great day.

Happy Rust Journey!🦀

Top comments (0)