Getting Started with Axum: Beginner-Friendly Project Setup Instructions
Get insights into Axum 7 folder organization by comparing it with ExpressJS.
Introduction👋🏻
Hi there fellow rustacean! I’m Utkarsh, and I’ve joined a start-up which requires the knowledge of rust (Axum) for their backend role, as I was exploring different resources for learning Axum, it was pretty difficult to get my head around coming from a JS background.
So, in this series, I’ll write down what I’ve understood so that you’ll have a comparatively easier migration from JS / Java background!
But make sure to get all the basic understanding of rust beforehand by going through the book
Understanding a cargo project
Getting started
Just as creating an ExpressJs app, we first create a new cargo
project with the command
cargo new backend
Cargo is a package manager and build tool for Rust. You can think of it roughly as npm
Understanding Folder Structure and Dependencies
In a clean cargo project, you have 2 directories src
and target
along with a cargo.toml
file
/src
This is the directory under which you’ll have all of the code of the application. It has the main.rs
which is the entry point of the rust project.
/target
This is the directory that stores the outputs of the binary created when you run/build your project. The code is first compiled and then executed
Cargo.toml
You can think of it like package.json
. It’s where you can find the details about your project and the dependencies that are used. Once you install some packages, there’s also a Cargo.lock
file that’s generated which can be related to package-lock.json
To add a new dependency you can either go to crates.io (think of it like npmjs for rust) or if you have some knowledge of the package names you can directly run
cargo add package_name --features feat1,feat2...
Now there’s a —features
flag there that you might be wondering about, In Cargo, the --features
flag is used when adding a dependency with optional features. Features in Cargo are a way to enable or disable specific functionality or dependencies within a crate. They allow for flexibility, letting you tailor how a package behaves in your project without bloating it with unnecessary code or dependencies.
Unlike npm
which will install all the optional dependencies unless told explicitly, cargo
keep the project minimal and give us the ability to install only those optional features which we’re going to use!
Now that you understand how the project is structured, we can move on to installing the dependencies required for our axum backend
Adding Dependencies
Now we’ll be requiring 3 main dependencies to get started with a basic project
1. Axum (axum = "0.7.9"
)
What it does:
Axum is a framework for building web servers in Rust, similar to how you'd use Express.js in JavaScript or Spring Boot in Java. It handles HTTP requests and responses, routing, and other web-related tasks.
Why we need it:
To set up a backend that listens for incoming requests, processes them, and sends back responses.
It makes building APIs easier by providing tools like routing and middleware.
Think of Axum as the "backbone" of our backend application, just like Express.js would be for a Node.js app.
2. Serde (serde = { version = "1.0.216", features = ["derive"] }
)
What it does:
Serde is a library for serializing and deserializing data. Serialization means converting data (like a Rust struct) into a format that can be sent over the network (like JSON). Deserialization does the opposite: it converts JSON or other formats into Rust objects.
Why we need it:
To handle JSON data in our backend. For example:
Parse incoming JSON data from a client into a Rust struct.
Convert Rust structs into JSON to send as responses.
The
derive
feature allows us to automatically generate code for this serialization/deserialization process, so we don’t have to write it ourselves.
JS comparison:
If you’ve used JSON.stringify
and JSON.parse
in JavaScript, Serde does something similar but in a much more structured way.
3. Tokio (tokio = { version = "1.42.0", features = ["rt-multi-thread", "macros"] }
)
What it does:
Tokio is an asynchronous runtime for Rust. It’s like the Event Loop in JavaScript or the Executor in Java. It allows you to handle multiple tasks (like handling many incoming HTTP requests) at the same time without blocking the application.
Why we need it:
Axum is built on top of Tokio, so it’s required to run the web server.
The
rt-multi-thread
feature enables multi-threaded performance, which is useful for handling many simultaneous requests efficiently.The
macros
feature allows you to use certain features of Tokio more easily in your code.
JS comparison:
Think of Tokio as async/await
in JavaScript. It enables concurrency without explicitly managing threads.
Conclusion
Congratulations, you now have an Axum 7 project setup, and you now understand the folder structure of the project, let’s build a Hello World app in our next blog!
Until then, Adios👋🏻