Comparing Two Most popular Rust frameworks (Actix-web and Rocket)

Zain Bawa
3 min readJun 21, 2020

--

Rust being the most popular language with developers for its open-source development and performance these days is the most favored contender for microservices and API creation. The language has a few web frameworks for API Creation but in this article, we will talk about the two most popular ones Actix-web and rocket. Both of these frameworks seem to have a good developer experience but Rocket is still not available on stable rust release, you have to switch to nightly to play around.

before we dig deeper lets riffle through the term framework. A framework is a combination of various dependencies and code snippets that makes it quick and effortless to build test and deploy code. when choosing the best framework for the purpose we tend to look at support for customization, flexibility, extensibility, security, compatibility with other libraries, etc. and these will be aspects we will be comparing when talking about the two most popular Rust server-side frameworks.

Let's start with activity in the developer's community

Image Sourced from rust.libhunt

looking at these numbers both the frameworks have some close competition in some aspects but actix-web is a clear winner in terms of new feature development and frequency of commits.

Talking About Performance and Speed

In terms of performance according to tech empower benchmark actix-web beats rocket with a huge margin, the same is the story with surfrago.blog where they tested three APIs rocket, actix-web and tower, and rocket was the worst performer.

Sourced from surfragos.blog

The results in the above picture may not be very accurate but they do give us an idea of the difference between both actix and rocket in terms of performance.

Individual Features

Rocket

It is one of the most mature production-ready full-stack Rust web frameworks which helps you write secure web applications without sacrificing, flexibility, and type safety.

  • supports implicit JSON via Serialize and deserialize and doesn't require external dependencies.
  • Internal Form handling and automatically type checks URLs which avoids code break by not letting bad requests through.
  • provides built-it templating support.
  • allows to easily view, add, remove cookies with or without encryption.
  • Provides built-in testing support to easily run unit tests.
  • Helps enforce both type and API header validation through request guard blocks.

Rocket makes extensive use of Rust code generation tools and its super easy to get started. Here is how a simple server that responds to get requests looks like:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> &'static str {
"This is how easy it is!"
}

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

Actix

Actix is a server-rendered framework based on a powerful rust actor framework, it is built to be usable and lightweight.

  • Supports both HTTP/1.x and HTTP 2.0 protocols.
  • Support asynchronous request handling, which makes it possible to handle multiple concurrent requests easily.
  • provides client and serverside web-socket support.
  • Able to serve both static and dynamic assets via OpenSSL or rustls.

Getting started with Actix-web which is built on top of actix core is simple too, you just need to understand a few more concepts than what you when getting started with Rocket. Here is how a simple server serving get-requests look like:

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

async fn index() -> impl Responder {
HttpResponse::Ok().body("I am actix-web, dont be scared!")
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/again", web::get().to(index2))
})
.bind("127.0.0.1:8088")?
.run()
.await
}

Conclusion

In conclusion, I would say it heavily depends on your choice if you want to build elegant web applications with emerging standards rocket is your go-to option but it can handle so many connections at any given instance as it is not async yet. actix-web, on the other hand. Acitx being built on rust actor framework provides top-notch performance. If you want to pack your microservice with immaculate performance Actix protocols low overhead will harmoniously mesh with your requirement.

--

--

Zain Bawa

A Technology enthusiast from Lancaster University. Favourite Topics: Big Data, Cloud, AI