Intro to Web Development with Rust for NodeJS Developers - First steps for a NodeJS developer to do web programming in Rust.
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 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.
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.
Rust depends almost completely on Rust itself. Just the compiler is using
llvm libraries, which are written in C and C++.
It was and is a design decision not to include a standard http library in Rust. The OSI layer is therefore covered differently:
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 is implemented in the Rust Core. The current web frameworks (actix and rocket) are implementing everything up until HTTP though. So you don’t need to care where this is coming from.
Node is using npm for its package management:
npm installis installing dependencies
npm run xyzis executing scripts inside the
On the Rust side,
cargo is handling everything related to your project:
cargo new NAME --binis creating an application
cargo new NAME --libto create a library
cargo runis executing the code
cargo buildis creating an executable
cargo testis running all tests inside the project
There is an open PR 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 iwhich fetches the packages right away, and will add it to the
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 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. 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.
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. A library called Tokio is already offering an asynchronous run time. You can track the progress on asynchronous programming in Rust over at areweasyncyet.
curl <a href="https://sh.rustup.rs" target="_blank">https://sh.rustup.rs</a> -sSf | sh
cargo new web-app --bin
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).
.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().
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.
reqwestis returning a Response struct (type) which implements certain methods. The documentation is, as with so many crates, excellent. So have a look here.
returnto 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.
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 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, Rust is making you a better developer, so the road will be long at times, but always worth it.
In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.
Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.