

24 days from node.js to Rust
source link: https://vino.dev/blog/node-to-rust-day-1-rustup/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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
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
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.
$ 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.
[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.
Recommend
-
10
In this article we will build a simple command line program that returns the word count of a file. This will essentially be a simpler version of the Unix utility wc , written in Rust. The goal of this articl...
-
6
Why using WebAssembly and Rust improves Node.js performance – IBM Developer By Josh Hannaford Updated September 1, 2020 | Published March...
-
6
I tried learning OpenGL in 7 days - using Rust3,157 views•Premiered Apr 3, 2021
-
6
This week in Fluvio, I want to talk about an interesting problem I encountered while implementing a Batch Producer API for the Fluvio client. As part of our feature development process, we update each of our language clients with new APIs for...
-
11
node.js是一个非常流行的JavaScript运行时,用于编写后端应用程序。 它的灵活性和非阻塞性使其成为编写API首要选择。由于它是脚本语言,JavaScript可能比较慢。 但由于V8的优化,它足以实现实际应用。 也就是说,Node.js对计算密集任务不利; 由于它是单...
-
4
Technical Articles
-
4
I'll admit it, I've never really used Rust. It's interested me for a while, but I never gave myself an excuse to use it until I had this idea. It's not very original by any stretch of the imagination to do this kind of thing, but this is how...
-
7
30DaysRust (2 Part Series) Yesterday was just a warmup, but today actually felt easier than yesterday, probably b...
-
6
30DaysRust (3 Part Series) I took a few days off from this challenge due to some personal things, but I came into...
-
7
Node.js 开发者的 Rust 入门指南更新日期: 2022-01-28阅读量: 27标签: node分享扫一扫分享
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK