So for the longest time, my goal has been to get into Rust engineering and transition into becoming a full-fledged Rust Engineeer. But I would keep procrastinating and putting it off. Whenever I did decide to get into it, I would just start reading through the Rust Programming Language book (https://doc.rust-lang.org/book/) and not really get anywhere. Yesterday, I decided to take a different approach.
Usually, people who want to achieve a goal X, take steps towards achieving it and the learning happens on the way. For example, if your goal is to play Moonlight Sonata on the piano and you have never touched a piano in your life, you would break it down into a roadmap of simpler songs first. First learn the CDEFABC, then minor keys, then Happy Birthday, Twinkle Twinkle Little Star, Chopsticks, so on and so forth until you can comfortably play Moonlight Sonata. I call this strategy Learning through Iteration and Improvement or LI2 for short.
So how do I implement LI2 here? In order to become a strong Rust engineer, I need to get comfortable with completing programming tasks using Rust. So to achieve this, I decided to build a simple Rust script for pulling data from the PokeAPI (https://pokeapi.co/) and displaying it to the console.
Sounds simple right? On Python this would have been at best a 4 line script:-
import requests
response = requests.get("https://pokeapi.co/api/v2/pokemon/ditto")
data = response.json()
print(data)
But on Rust it proved a bit more of a hassle. First I had to install the Reqwest and Tokio libraries using cargo, and then understand the example code they had on there:-
let body = reqwest::get("https://www.rust-lang.org")
.await?
.text()
.await?;
println!("body = {body:?}");
What is that ::? Why are there 2 awaits in there? If I am writing this into fn main() how can I ensure that main becomes an async function and has clear expectations on return outputs? These are all questions I had to answer step by step till the full code became clear to me. And so I wrote my whole code:-
const POKE_GET_URL: &str = "https://pokeapi.co/api/v2/pokemon/ditto";
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let body = reqwest::get(POKE_GET_URL).await?.text().await?;
println!("{}", body);
Ok(())
}
In conclusion, the whole process which would have taken an expert 5 minutes to code took me an hour. But I loved every second of it. It felt good to stop relying on AI or Stack Overflow to solve my problems and actually activate my brain to go read the documentation and figure out how to map their explanations to my usecase.
Top comments (0)