Rust for JavaScript Developers - Tooling Ecosystem Overview

  • 时间: 2020-06-29 09:49:26

This is the first part in a series about introducing the Rust language to JavaScript developers.

I find it easier to understand something new if it was explained in terms of something I already know - I thought there might be others like me :)

Here’s the tl;dr version:

ToolJavaScriptRust
Version Managernvmrustup
Package ManagernpmCargo
Package Registrynpmjs.comcrates.io
Package Manifestpackage.jsonCargo.toml
Dependency Lockfilepackage-lock.jsonCargo.lock
Task Runnernpm scripts, gulp etcmake, cargo-make
Live Reloadnodemoncargo-watch
LinterESLint, TSLint, JSLintClippy
FormatterPrettierrustfmt
Dependency Vulnerability Checkernpm auditcargo-audit

Setup

Rust is installed using therustupcommand. rustup is similar tonvmin Node.js. You can use it to install and manage multiple versions of Rust and more.

Cargo

Installing Rust using rustup also installs Cargo similar to how installing Node.js also installs NPM. Cargo is Rust’s package manager and would feel very familiar if you’ve used NPM before.

Rust’s packages are called “crates”, and they’re downloaded from thecrates.ioregistry similar to how NPM packages are downloaded fromnpmjs.com.

NPM while primarily a package manager, is also used as a task runner using thenpm scriptsfeature. Cargo has builtin support for common tasks like running code, building code etc. Cargo has features likeworkspaces(similar tolerna),dependency overrides(similar topatch-package) out of the box. It is also a test runner (similar to mocha, jest etc), benchmark runner etc.

So basically, Cargo is NPM on steroids!

Project Setup

Creating a new project is done by running

$ cargo new hello_rust

This is somewhat similar tonpm init. This creates a directory called “hello_rust” with the following files:

hello_rust├── .git├── .gitignore├── Cargo.toml└─┬ src  └── main.rs

Cargo.toml

This is the package manifest file similar to package.json. The lock file (package-lock.json equivalent) is named Cargo.lock. Opening the Cargo.toml, you’ll see a familiar layout:

[package]name = "hello_rust"version = "0.1.0"authors = ["sheshbabu"]edition = "2018"[dependencies]

The[package]tablecontains metadata like the crate name, author, keywords etc. The[dependencies]table is similar to thedependencies object in package.json. Cargo.toml also supports[dev-dependencies]similar todevDependencies.

Dependency Management

Installing a new dependency is done by manually editing the Cargo.toml file, adding the dependency under[dependencies]and runningcargo build. For example, if we want to install the “serde” crate, we need to edit the Cargo.toml file as follows:

[package]name = "hello_rust"version = "0.1.0"authors = ["sheshbabu"]edition = "2018"[dependencies]+ serde = "1.0.106"

and run

$ cargo build

Similarly, to remove or update a dependency, we need to manually edit the Cargo.toml file and runcargo build. I was initially confused by the existence of thecargo installcommand but it turned out to be an equivalent ofnpm install -g.

If you want something similar tonpm install,npm updateornpm uninstall, you can installcargo-editwhich enhances Cargo withcargo add,cargo rmandcargo upgradesubcommands.

You can also specify thedependency version using patternssimilar to NPM.

Development tools

Task Runner

Cargo supports running common tasks like build, run, test etc. But if you want something similar to NPM scripts, you can use make orcargo-make

Live Reload

Nodemon is an essential tool for Node.js development - it watches for changes to files and automatically restarts the application.cargo-watchis the equivalent in Rust world.

Linter and Formatter

Rust has builtin linter calledClippyand formatter calledrustfmt. They’re equivalent to ESLint and Prettier in JS ecosystem. Precommit hooks can be managed usingcargo-husky.

Vulnerability Checking

Scanning for vulnerabilities in dependencies is done usingcargo-auditand is very similar tonpm audit.

Thanks for reading! :)