Touching the top of rust

So, for the study I’m following we have one day in the week to work on hobby projects. But because I don’t want to waste that time on stuff I otherwise do, I use it to try out new frameworks, tools and languages. This is also where and why I started experimenting with Rust. A blazing fast language that can be used anywhere I mean anywhere 🚀. At the moment of writing, my Twitter timeline slowly fills itself with Rust lovers, so yeah I don’t want to miss that train.

My adventure

Starting a new language can sometimes be pretty straightforward, switching from PHP to NodeJS or maybe even Ruby doesn’t have to be hard, you can easily create something useful in a small time. But this time with learning Rust, it gets a bit harder. Mostly due to my not so advanced low level code knowledge. When I first looked at the syntax, my mind kinda blew. Just like I was a kid again looking into the source code of every webpage I could find. The hackerman I was huh.

To not get to overwhelmed by all the information that’s in front of me, I decided to learn the language whilst creating something. This proces can be supported by a good book or some video serie whatever floats your boat. I chose the book, mostly due to the fact that I can look back something without having to perfectly scroll to that tiny fragment I have to see. With the book I followed I got to make a newsletter api that’s connected to a database. Pretty straightforward to do that in any other language already known to me. So as the good student I am, I started following the steps the book provided like installing rustup and starting a new project. I fired up my code editor cracked my hands to finally make some code.

The code

So, following the process of the book I figured out how to add crates, how to create a Web server but most importantly how to separate code. Because at the start of the project, I did all the requests and route handling and server starting in the main.rs file. Of course no one wants to read code in one file, so after separating the code and making it kinda readable, I also started figuring out how it worked. I went from this main.rs

use actix_web::{web, App, HttpRequest, HttpServer, Responder};
 
async fn greet(req: HttpRequest) -> impl Responder {
    let name = req.match_info().get("name").unwrap_or("World");
    format!("Hello {}!", &name)
}
 
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(greet))
            .route("/{name}", web::get().to(greet))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

To this

use rustplayground::configuration::get_configuration;
use rustplayground::startup::run;
use sqlx::PgPool;
use std::net::TcpListener;
 
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let configuration = get_configuration().expect("Failed to read configuration.");
    let connection_pool = PgPool::connect(&configuration.database.connection_string())
        .await
        .expect("Failed to connect to Postgres");
 
    let address = format!("127.0.0.1:{}", configuration.application_port);
    let listener = TcpListener::bind(address)?;
    run(listener, connection_pool)?.await
}

As you can see, the Routes are separated, the port binding is done dynamically and the code is more robust. While following the book, I not only learned more of the language, but also of the things you have to think about. Port binding, database selecting health checking all subjects I touched in my learning process.

The project I worked on can be found on my GitHub page, at this repository