Using Rust in Web Development

Hi all! Probably many of you already know about the Rust programming language. But if anyone doesn’t know, Rust is a general purpose multi-paradigm compiled programming language. Everyone knows that Rust is mainly used for creating CLI applications and systems programming, but it has other areas. For example – the development of video games, but we are not talking about that. Today I would like to discuss web programming in Rust.

Rust is a productive and secure programming language, it is reliable, fast and convenient. Therefore, it began to be used for web programming. There are basically two frameworks for building web applications for this, Actix Web And Rocket. Personally, I often use Actix-Web because of its high performance and ease of writing code on it.

Actix-Web is a powerful framework for building Rust web applications. It is very light and comfortable. Its convenience is speed in a single executable file. You just run the .exe file and you have the whole application in one file than PHP, in which half of the folders and files are occupied by Docker and Composer. Actix-Web is based on another library (Actix, yes, not Web, just Actix), it works on asynchronous and actor functions.

use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello World!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(hello)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

As you can see, Actix-Web is very simple, first we create the hello() function and set the “get” macro to it, it denotes a request (GET / POST), then we simply write in the HttpResponse function that will answer us “Hello World!”. Next we have an asynchronous main() function, then nothing special, create a server, run it and you’re done! At the address http://127.0.0.1:8080/ we see the inscription “Hello, World!”. Actix Web site: https://actix.rs/.

Another framework, Rocket, is quite interesting. Rocket also convenient and fast, but its trick is that it is expandable. Its architecture allows you to write additions to it or even just make changes to the framework itself (in your project). This is where Rocket comes in handy, for example, you can write your own database for Rocket, or add things to Rocket that are missing in other frameworks. And even more so, the very name of the framework hints at the fact that it is fast (like a rocket). Although in my experience of using Rocket in terms of speed, it is inferior to Actix-Web.

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();
}

The syntax is slightly different from Actix-Web, in particular the server differs very much, by default, the port and address of the server is localhost: 8000, but it can be changed (logically), By the way, the handling of the “index” function to the server is slightly different, we specify the routing for it the main page and in the macro of the function, and in the handling of the function. The launch() method launches our web application. All is ready! Rocket website: https://rocket.rs/.

In fact, there are a lot of frameworks/libraries for building Rust web applications. But the most popular and best are Actix-Web and Rocket. In general, web programming in Rust is convenient, simple, and productive. Happy Rust development everyone, lambda was with you.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *