24 days from node.js to Rust

24 days from node.js to Rust

Authors
Jarrod Overson

December 1, 2021

Introduction

A guide to Rust from a node.js developer’s perspective.

Welcome to our 24-post series on getting started with Rust! Each day until Christmas (December 25th 2021) you’ll find another post taking something you know how to do in node.js and translating it to Rust. Today kicks off with you setting up Rust via a tool similar to nvm, rustup. Next up we’ll tackle cargo and setting up VS Code. Later on we’ll go over language gotchas, rewrite common JavaScript tasks in Rust, and go over popular third party dependencies.

Wait, why does anyone need to learn anything but JavaScript?

I love JavaScript. I’ve been coding JavaScript it since I first saw it in Netscape. I’ve written more JavaScript than any other language. I really do love it, which means I know how it falls short. It’s fast, but not that fast. It’s easy to write, but easy to screw up. Large projects become unwieldy quickly. TypeScript helps scale JavaScript, but it adds its own complexity and still doesn’t make anything faster. Serverside JavaScript also relies on node.js. If you want to distribute something self-contained, there aren’t great answers.

When you start stretching passed what JavaScript is best at, it’s helpful to have another language to turn to.

Why Rust?

You could use C, C++, C#, Go, Java, Kotlin, Haskell or a hundred others. Rust is notoriously difficult even for system programmers to get into. So why bother with Rust? Look at it this way: you already have JavaScript, a high level language that’s good enough to run just about everything everywhere. If you’re picking up a new language, you might as well go to the extreme and pick a no-compromise powerhouse.

Also, WebAssembly.

Rust’s tooling and support for WebAssembly is better than everything else out there. You can rewrite CPU-heavy JavaScript logic into Rust and run it as WebAssembly. Which basically makes you a superhero. With JavaScript and Rust, there’s nothing you can’t handle.

Disclaimer

This guide is not a comprehensive Rust tutorial. It’s meant to bootstrap experienced node.js users into Rust. We’ll take common node.js workflows and idiomatic JavaScript and TypeScript and map them to their Rust counterparts. This guide tries to balance technical accuracy with readability and errs on the side of “gets the point across” vs being 100% correct. When something is glossed over, we’ll add links for those looking to dive deeper.

Post questions and comments to me on Twitter @jsoverson or @vinodotdev and join our Discord channel!

Day 1: Installing rust with rustup

nvm (or nvm-windows) are indispensible tools. They manage seamlessly installing and switching between versions of node.js on the same system.

The equivalent in Rust’s world is rustup.

Rustup manages your Rust installation as well as additonal targets (like WebAssembly) and core tools like cargo (Rust’s npm), clippy (Rust’s eslint), rustfmt (Rust’s prettier).

After installing rustup, run it without any subcommands and explore what it has to offer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$ rustup
rustup 1.24.3 (ce5817a94 2021-05-31)
The Rust toolchain installer

USAGE:
    rustup [FLAGS] [+toolchain] <SUBCOMMAND>

FLAGS:
    -v, --verbose    Enable verbose output
    -q, --quiet      Disable progress output
    -h, --help       Prints help information
    -V, --version    Prints version information

ARGS:
    <+toolchain>    release channel (e.g. +stable) or custom toolchain to set override

SUBCOMMANDS:
    show           Show the active and installed toolchains or profiles
    update         Update Rust toolchains and rustup
    check          Check for updates to Rust toolchains and rustup
    default        Set the default toolchain
    toolchain      Modify or query the installed toolchains
    target         Modify a toolchain's supported targets
    component      Modify a toolchain's installed components
    override       Modify directory toolchain overrides
    run            Run a command with an environment configured for a given toolchain
    which          Display which binary will be run for a given command
    doc            Open the documentation for the current toolchain
    man            View the man page for a given command
    self           Modify the rustup installation
    set            Alter rustup settings
    completions    Generate tab-completion scripts for your shell
    help           Prints this message or the help of the given subcommand(s)

DISCUSSION:
    Rustup installs The Rust Programming Language from the official
    release channels, enabling you to easily switch between stable,
    beta, and nightly compilers and keep them updated. It makes
    cross-compiling simpler with binary builds of the standard library
    for common platforms.

    If you are new to Rust consider running `rustup doc --book` to
    learn Rust.

rustup show will show you what is currently installed.

rustup completions will help you enable CLI autocompletion for tools like rustup and cargo.

rustup component lets you add additonal components.

rustup update will update you to the latest version.

rustup install stable|nightly|1.57 will install a specific version or the latest stable/nightly versions.

By default, rustup will install the latest version of rust and cargo and you should be ready to go right away. Give it a shot with.

1
2
3
4
5
$ rustc --version
rustc 1.57.0 (59eed8a2a 2021-11-01)

$ cargo --version
cargo 1.56.0 (4ed5d137b 2021-10-04)

If it doesn’t work, you may need to restart your shell to update your PATH.

rust-toolchain.toml

Specifying your toolchain with rustup is easy enough. As you get deeper, you may get into configurations where different projects require different toolchains or Rust versions. That’s where rust-toolchain.toml comes into play. Specify your project’s required toolchain, targets, and supporting tools here so that cargo and rustup can work automagically, e.g.

1
2
3
[toolchain]
channel = "1.56.0"
components = [ "rustfmt", "clippy" ]

Next steps

Next up we’ll take a look at cargo, Rust’s npm and the additional tools that will help reach parity with common workflows: Day 2: From npm to cargo.

You can reach me personally on twitter at @jsoverson, the Vino team at @vinodotdev. Don’t forget to join our Discord channel to discuss Rust with other people going through this same transition.