CHAPTER 26
Beginner
Web Development with Rust
Updated: May 18, 2026
5 min read
# CHAPTER 26
Web Development with Rust
1. Chapter Introduction
Rust is rapidly becoming the language of choice for building high-performance backend infrastructure. Frameworks likeActix Web, Axum, and Rocket routinely top the charts in the TechEmpower Web Framework Benchmarks, massively outperforming Node.js, Python, and even Go. In this chapter, we will build a modern REST API using Actix Web, handling HTTP routing, JSON serialization, and server initialization.
2. Learning Objectives
By the end of this chapter, you will be able to:- Install and configure Actix Web and Serde.
- Start an asynchronous HTTP server.
-
Define routing endpoints (
GET,POST).
- Serialize Rust Structs into JSON responses.
- Extract data from URL paths.
3. Setting Up the Project
We need two main dependencies:actix-web for the server, and serde for handling JSON.
Update your Cargo.toml:
toml
4. Creating a Basic HTTP Server
Starting a web server requires an Async runtime. Actix Web comes with its own runtime macro (#[actix_web::main]) built on top of Tokio.
rust
*Run this code, open http://localhost:8080/hello in your browser, and see the result!*
5. Returning JSON (The Magic of Serde)
REST APIs communicate using JSON. We don't want to format JSON strings manually. Instead, we use theserde crate to automatically serialize our Rust Structs.
rust
*Output when visiting /api/user:*
{"id": 101, "username": "alice_dev", "role": "admin"}
6. Extracting URL Path Variables
Often, clients request specific data, like/api/users/101. We can extract the 101 directly from the URL.
rust
7. Handling POST Requests
To handle aPOST request (e.g., a client sending a JSON payload to create a new user), we use #[post], derive Deserialize on our struct, and use web::Json as a parameter.
rust
8. Common Mistakes
-
Forgetting to Register Services: Writing a route function like
async fn login()but forgetting to add.service(login)inside theHttpServer::newclosure. The route will return a 404 Not Found.
-
Missing Serde Derivations: Trying to return
web::Json(mystruct)without adding#[derive(Serialize)]to the struct. The compiler will panic, saying the trait bound is not satisfied.
9. Best Practices
-
Extract Application State: Actix Web allows you to inject shared state (like a Database Connection Pool from Chapter 25) into
App::new().appdata(...). This allows all your routes to securely access the database without creating a new connection every time.
10. Exercises
- 1. Setup an Actix Web project.
-
2.
Create a struct
Product(id, name, price) and deriveSerialize.
-
3.
Create a
GETroute/productsthat returns a hardcodedProductas JSON.
- 4. Run the server and test it in your browser.
11. MCQs with Answers
Question 1
Which of the following is a highly popular, extremely fast Web Framework for Rust?
Question 2
What crate is the industry standard for converting Rust Structs into JSON?
Question 3
What macro is used to define a route that handles GET requests?
Question 4
What must you derive on a Struct to allow Actix to return it as a JSON response?
Question 5
In Actix, how do you specify the port and IP the server should listen on?
Question 6
If your route is #[get("/users/{id}")], what parameter type do you use to extract the id?
Question 7
What macro is used to define an endpoint that creates data on the server?
Question 8
What must you derive on a Struct to allow Actix to parse an incoming JSON payload into that Struct?
Question 9
If you define a route function but get a 404 Not Found error in the browser, what likely happened?
Question 10
Why are Rust backend frameworks so highly rated for enterprise architecture?
12. Interview Questions
-
Q: Explain how
Serdeworks with Actix Web to handle JSON serialization and deserialization seamlessly.
- Q: How does Actix handle massive concurrency? (Answer: It utilizes the Tokio async runtime, spawning lightweight tasks to handle incoming requests rather than heavy OS threads).