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.
Quick links
- Day 1: From nvm to rustup
- Day 2: From npm to cargo
- Day 3: Setting up VS Code
- Day 4: Hello World (and your first two WTFs)
- Day 5: Borrowing & Ownership
- Day 6: Strings, part 1
- Day 7: Syntax and Language, part 1
- Day 8: Language Part 2: From objects and classes to HashMaps and structs
- Day 9: Language Part 3: Class Methods for Rust Structs (+ enums!)
- Day 10: From Mixins to Traits
- Day 11: The Module System
- Day 12: Strings, Part 2
- Day 13: Results & Options
- Day 14: Managing Errors
- Day 15: Closures
- Day 16: Lifetimes, references, and
'static
- Day 17: Arrays, Loops, and Iterators
- Day 18: Async
- Day 19: Starting a large project
- Day 20: CLI Arguments & Logging
- Day 21: Building and Running WebAssembly
- Day 22: Using JSON
- Day 23: Cheating The Borrow Checker
- Day 24: Crates & Tools
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.
|
|
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.
|
|
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.
|
|
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.