How Do I Make a Web Server on Rust?

//

Scott Campbell

Creating a web server on Rust is a fantastic way to leverage the power and efficiency of this modern programming language. With its focus on performance and safety, Rust is an excellent choice for building robust and secure web applications. In this tutorial, we will explore the necessary steps to create a web server using Rust.

Setting up the Project

Before we dive into creating our web server, we need to set up our project. First, ensure that you have Rust installed on your machine. If not, head over to the official Rust website and follow the installation instructions for your operating system.

Once Rust is successfully installed, open your terminal or command prompt and create a new directory for your project. You can do this using the following command:


$ mkdir my-web-server
$ cd my-web-server

Now that we are inside our project directory, let’s initialize it as a Rust project using Cargo. Cargo is the package manager and build system for Rust projects.


$ cargo init --bin

This command will generate a new Rust project with a basic directory structure and a Cargo.toml file.

Adding Dependencies

In order to create our web server, we need to add some dependencies to our Cargo. Open the file in your preferred text editor and add the following lines:


[dependencies]
actix-web = "3"

We are using Actix-Web as our web framework of choice. It provides an easy-to-use API for building asynchronous web applications in Rust.

Save the Cargo.toml file and run cargo build in your terminal to fetch and build the dependencies.

Writing the Code

Now that we have our project set up and our dependencies installed, let’s write some code to create our web server. Create a new file called main.rs in the project root directory and open it in your text editor.

Add the following code to import the necessary modules:


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

This line imports the required modules from Actix-Web.

Next, let’s define a handler function that will handle incoming HTTP requests. Add the following code:


async fn hello() -> HttpResponse {
HttpResponse::Ok().body("Hello, World!")
}

This function will return an HTTP response with a body containing the text “Hello, World!”.

Now, let’s define our main function which will set up and start our web server. Add the following code:


#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(hello))
})
.bind("127.0.1:8080")?
.run()
.await
}

This code sets up an HTTP server using Actix-Web on localhost:8080. It defines a single route that maps to the hello handler we defined earlier.

Running the Web Server

We are now ready to run our web server! Open your terminal or command prompt and navigate to your project directory. Run the following command:


$ cargo run

If everything is set up correctly, you should see output indicating that the server is running. Open your web browser and navigate to http://localhost:8080.

You should see the “Hello, World!” message displayed.

Conclusion

Congratulations! You have successfully created a web server using Rust and Actix-Web.

You can now extend this basic example to build more complex web applications or APIs using Rust’s powerful and safe features. Happy coding!