Intro to Web Development with Rust for NodeJS Developers
Tags: rust, node
Published: 2019-03-18 21:37:37

---
title: Intro to Web Development with Rust for NodeJS Developers
published: true
description: First steps for a NodeJS developer to do web programming in Rust
tags: rust, nodejs
series: Practical Web Development with Rust
---
>You can find the second article in this series ("deploy your first Rust app") [over here](https://dev.to/gruberb/web-programming-in-rust-02x-deploy-your-first-app-1k05).

Rust is different. You can pick up Python or Ruby over the weekend, create a first CRUD application and be happy with the results.

With Rust… with Rust you will struggle to pass a String to a different method, change and return it. You then will order the Rust book, see its size, *sigh* and get started.

After a few weeks of fighting through the book after work, you give up and wait until someone else creates an easy-to-follow tutorial.

### Here is your “easy” tutorial
I struggled with the same problems. Life circumstances however gave me a few months time on my hands to really focus on Rust.

What follows is a first overview, concept, and paths to follow. In the coming weeks and months, I’ll publish a series of articles to help you to get from concept to product.

### NodeJS vs Rust
After installing them (I chose brew for macOS in this example, the method doesn’t matter), the underlying stack looks different. NodeJS needs V8, the runtime engine from Google, and bindings to the JavaScript library to run JavaScript code.

Rust depends almost completely on Rust itself. Just the compiler is using `llvm` [libraries](https://llvm.org/), which are written in C and C++.

![os_difference_node_rust](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/rust.png)

### How much "web" is in Rust?
It was and is a design decision [not to include a standard http library](https://users.rust-lang.org/t/announcing-the-http-crate/12123) in Rust. The OSI layer is therefore covered differently:

![osi_layer_rust_node](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/rustnode.png)

Node covers the whole stack, and offers with Koa and Express, two well-known and “rock-solid” web frameworks which help you to build applications on top of HTTP.

On the Rust side of things, just [TCP](https://doc.rust-lang.org/std/net/struct.TcpStream.html) is implemented in the Rust Core. The current web frameworks ([actix](https://actix.rs/actix/actix/) and [rocket](https://rocket.rs/)) are implementing everything up until HTTP though. So you don’t need to care where this is coming from.

If you want to use pure HTTP calls without any larger framework, you can install “crates” (equivalent to npm packages in the Node world) which implement the HTTP protocol (like [hyper](https://github.com/hyperium/hyper) and [tiny_http](https://github.com/tiny-http/tiny-http)).

### npm vs cargo
Node is using npm for its package management:

- `npm install` is installing dependencies
- `npm run xyz` is executing scripts inside the `package.json`

On the Rust side, `cargo` is [handling everything related to your project](https://doc.rust-lang.org/cargo/):

- `cargo new NAME --bin` is creating an application
- `cargo new NAME --lib` to create a library
- `cargo run` is executing the code
- `cargo build` is creating an executable
- `cargo test` is running all tests inside the project

There is an [open PR](https://github.com/rust-lang/cargo/issues/5586) to add `cargo add` to install dependencies. Right now you have to add them by hand to your `Cargo.toml` file. As you see, you don’t need to include scripts in a `package.json` to run tests or build and test your application.

> Mindset change: Cargo is fetching the packages after `cargo run`, and just if the version changed. So the first time it will fetch all packages, the second time just when a change in the version number happened. Unlike `npm i` which fetches the packages right away, and will add it to the `package.json` with the `save` notation.

### Ecosystem
Node is not successful for no reason. The ecosystem is rich and flourishing. Rust is still developing, but has already many great “crates”. The website [arewewebyet.org](http://www.arewewebyet.org/) is tracking the progress and showing you interesting packages in the Rust world.

There is also an attempt to create an official Rust Web Framework, called [Tide](https://github.com/rustasync/tide). It is already pretty mature and can be used for side projects. Feel free to contribute and help craft a great environment for web development in Rust.

### Asnyc programming aka Promises aka Futures
Nodes killer feature are Promises. Although not always easy to understand and handle, Promises and the event loop are what makes Node so lucrative.

Rust is also implementing an asynchronous mechanism, which are not yet in the final version. They are called [Futures](https://github.com/rust-lang-nursery/futures-rs). A library called [Tokio](https://tokio.rs/) is already offering an asynchronous run time. You can track the progress on asynchronous programming in Rust over at [areweasyncyet](https://areweasyncyet.rs/).

### How to get started?

1. Install Rust: `curl https://sh.rustup.rs -sSf | sh`
2. Create a new project: `cargo new web-app --bin`
3. `cd web-app`

Now you can choose your web framework of choice. You can either start with [rocket](https://rocket.rs/) or [actix](https://actix.rs/). You can follow the tutorials on the website to get a first web application running.

### Heads up: undefined, borrowing and types
To not to get frustrated until my next post, here are the main four things about Rust you will have to get used to (which are quite awesome after a while).

![updated_reqwest_example](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/code.png)

1. There is no undefined. Rust has no real null value. This is a feature. Rust has a type called [Option](https://doc.rust-lang.org/std/option/index.html), which encapsulates either the return value or None. In this case, we use the [Result](https://doc.rust-lang.org/std/result/#result-and-option) type to return a value. You could, instead of returning a String, return an Option which might have a String value, or None if the website we are fetching from doesn’t contain any text. An easy solution, which you should not use in production, is to `.unwrap()` results to get the String out of the encapsulation. [Homework: Try to check if the GET request errored and return an Error in this case instead of Ok().](https://doc.rust-lang.org/std/result/#result-and-option)
2. You must have heard about [borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html). In short: Every assignment (=) to a non trivial type (everything which doesn’t have a fixed size) moves the ownership over. The method `fetch_text()` is not taking ownership over the url but just using a reference to it (via the `&` ). [Homework: Figure out why this code here fails and how to solve it.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d7527a2b8570393dd70c2083172d621a)
3. You always have to know which type is returned from a method. In this example, our HTTP crate `reqwest` is returning a Response struct (type) which implements certain methods. The documentation is, as with so many crates, excellent. [So have a look here](https://docs.rs/reqwest/0.9.11/reqwest/struct.Response.html#method.text).
4. You don’t have to type `return` to return a value from a method. Just don’t put a `;` at the end of a statement and this becomes your return value.

Use this example to play around, see where you can get errors and learn to fix them.

### Is this all?
Two opinions:

- “Sadly no!”
- “No, but that’s a good thing!”

You actually have to learn a decent amount of Rust to get started. This is what I am here for. In the next few days, weeks and months I will cover the basics up until creating a solid web application.

Up until then, I can recommend the [Rust track on Exercism.io](https://exercism.io/my/tracks/rust) and the Rust Book which you can find also in a paper version at your local book store or at Amazon.

[As mentioned in my first article](https://medium.com/@gruberbastian/moving-from-nodejs-to-rust-in-2019-62c0b85d2fa7), Rust is making you a better developer, so the road will be long at times, but always worth it. Follow me on [twitter](https://twitter.com/byteadventures), [Medium](https://medium.com/@gruberbastian) or on [dev.to](https://dev.to/gruberb) to stay up to date!