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:
Tool | JavaScript | Rust |
---|---|---|
Version Manager | nvm | rustup |
Package Manager | npm | Cargo |
Package Registry | npmjs.com | crates.io |
Package Manifest | package.json | Cargo.toml |
Dependency Lockfile | package-lock.json | Cargo.lock |
Task Runner | npm scripts, gulp etc | make, cargo-make |
Live Reload | nodemon | cargo-watch |
Linter | ESLint, TSLint, JSLint | Clippy |
Formatter | Prettier | rustfmt |
Dependency Vulnerability Checker | npm audit | cargo-audit |
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.
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!
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
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.
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 install
command but it turned out to be an equivalent ofnpm install -g
.
If you want something similar tonpm install
,npm update
ornpm uninstall
, you can installcargo-editwhich enhances Cargo withcargo add
,cargo rm
andcargo upgrade
subcommands.
You can also specify thedependency version using patternssimilar to NPM.
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
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.
Rust has builtin linter calledClippyand formatter calledrustfmt. They’re equivalent to ESLint and Prettier in JS ecosystem. Precommit hooks can be managed usingcargo-husky.
Scanning for vulnerabilities in dependencies is done usingcargo-auditand is very similar tonpm audit.
Thanks for reading! :)