For the first time ever Rust helps me with my personal problem!🦀
Today, GitHub sent me an email to set up 2FA (two-factor authentication). After completing it, they gave me backup codes, suggesting I print them out or write them down and then delete the text file.
While writing the codes down, I realized they were hexadecimal numbers. That got me thinking: “Why not convert these to decimal too?” At first, I considered using an online converter, but with 32 codes, it would've taken ages. Then it hit me: "I know Rust—why not write a quick script to handle this?"
Here’s the small Rust code I wrote:
use std::fs;
const PATH: &str = "test.txt";
fn main() {
let text = fs::read_to_string(PATH).unwrap();
let mut code_vec: 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);
}
In no time, I had a list of decimal numbers! It may seem small, but for me, it was a huge win because it was the first time I used programming to solve a personal issue.
This made me appreciate Rust (or any programming language) even more—sometimes, the smallest programs can save a ton of time and effort.
So, if you're learning a language, keep going. You'll be surprised how it can help in unexpected ways!
Have a great day.
Happy Rust Journey!🦀