A Web App in Rust - 01 Getting Started

krowemoh

Nivethan

Posted on October 25, 2020

A Web App in Rust - 01 Getting Started

Hello, World! Let's build a hacker news clone in rust and see what that entails. I've broken the tutorial into chapters based on my own ideas of how to structure a web application and I've also broken it down based on how I want to learn.

In a lot of places you'll see things explicitly or even wrongly done because it makes more intuitive sense. Stuff like not hashing passwords and errors not being handled are done because I think it makes it easier to see the entire forest instead of getting into the weeds. Once we have the gist of the application working, we'll circle back to fix our mistakes.

Caveat - I'm very much a beginner in rust so everything has a heaping of salt next to it!

Installing Rust

The first step may be the hardest! Install and get Rust working. You can find more instructions on the Rust site.

https://www.rust-lang.org/tools/install

It is usually straightforward and I know on Linux that the install script works smoothly. I'd be happy to add any bugs anyone ran into while installing Rust. I ran into some issues using Windows Subsystem for Linux but unfortunately I don't remember them or their fixes.

Once installed, you can verify it by the below:

> rustc --version
rustc 1.47.0 (18bf6b4f0 2020-10-07)
Enter fullscreen mode Exit fullscreen mode

Easy peasy! (I hope the installation went well:)

IDE

I wholeheartedly suggest using an IDE, especially on Windows Subsystem for Linux. Rust takes a long time to compile on WSL so it's much better to get the IDE to highlight the errors beforehand.

I use Vim and YouCompleteMe with the rust language server installed. This works pretty well and will save a lot of the headaches of looking for dumb errors like not using enough colons or having misplaced periods.

Starting our Application

The next step is to start our application. Here we can use cargo. (This gets installed alongside rust, this is rust's package manager.)

> cargo new hacker-clone
     Created binary (application) `hacker-clone` package
Enter fullscreen mode Exit fullscreen mode

Now to make sure it compiles and runs.

> cd hacker-clone/
hacker-clone> cargo run
   Compiling hacker-clone v0.1.0 (hacker-clone)
    Finished dev [unoptimized + debuginfo] target(s) in 0.96s
     Running `target/debug/hacker-clone`
Hello, world!
Enter fullscreen mode Exit fullscreen mode

Done! We haven't done anything yet....but we did confirm that we can finally start doing something.

Starting with Actix

Now with Rust set up and working, we can start working on the web framework. The framework we'll be using is actix-web.

The first step is to include it in our Cargo.toml file.

./Cargo.toml

[dependencies]
actix-web = "3"
Enter fullscreen mode Exit fullscreen mode

Now, finally, finally, finally we can start editing some code.

Open up src/main.rs and add the following

./src/main.rs

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

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::to(|| {
                HttpResponse::Ok().body("Hello, World!")
            }))
    })
    .bind("127.0.0.1:8000")?
    .run()
    .await
}
Enter fullscreen mode Exit fullscreen mode

We include the parts of actix_web we need and we then start up our server with HttpServer::new().run().

We have set up our server to respond to the route, "/", with a http response of 200 which is OK, with the body being "Hello, World!".

You can see more options here:
https://docs.rs/actix-web/0.4.0/actix_web/struct.HttpResponse.html

Currently we have the route using an anonymous function but we could also name this function and pass it into the route. We can also instead of using the route option, use macros and services to register our endpoints.

You can see examples of this here:
https://actix.rs/docs/getting-started/

We also bound our server to the localhost IP address, 127.0.0.1 on port 8000. This means that our Rust web application will now respond to anything that comes in on port 8000 on the localhost.

Lets run it!

hacker-clone> cargo run
   Compiling hacker-clone v0.1.0 (/home/nivethan/bp/hacker-clone)
    Finished dev [unoptimized + debuginfo] target(s) in 13.86s
     Running `target/debug/hacker-clone`
Enter fullscreen mode Exit fullscreen mode

Now navigate to the below address from your browser:

http://127.0.0.1:8000/
Enter fullscreen mode Exit fullscreen mode

Voila! You should see our "Hello, World!" message. We have the barest hints of a website actually working. Don't blink, pretty soon we'll have an actual application.

Now to the next chapter!

💖 💪 🙅 🚩
krowemoh
Nivethan

Posted on October 25, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

A Web App in Rust - 02 Templates
webdev A Web App in Rust - 02 Templates

October 25, 2020

A Web App in Rust - 05 Database
webdev A Web App in Rust - 05 Database

October 25, 2020

A Web App in Rust - 04 Forms
webdev A Web App in Rust - 04 Forms

October 25, 2020