- Tooling Ecosystem Overview
- Variables and Data Types
- Functions and Control Flow
- Pattern Matching and Enums
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:
|Task Runner||npm scripts, gulp etc||make, cargo-make|
|Linter||ESLint, TSLint, JSLint||Clippy|
|Dependency Vulnerability Checker||npm audit||cargo-audit|
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.
NPM while primarily a package manager, is also used as a task runner using the npm scripts feature. Cargo has builtin support for common tasks like running code, building code etc. Cargo has features like workspaces (similar to lerna), dependency overrides (similar to patch-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 to
npm 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]
[package] table contains metadata like the crate name, author, keywords etc. The
[dependencies] table is similar to the dependencies object in package.json. Cargo.toml also supports
[dev-dependencies] similar to devDependencies.
Installing a new dependency is done by manually editing the Cargo.toml file, adding the dependency under
[dependencies] and running
cargo 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"
$ cargo build
Similarly, to remove or update a dependency, we need to manually edit the Cargo.toml file and run
cargo build. I was initially confused by the existence of the
cargo install command but it turned out to be an equivalent of
npm install -g.
If you want something similar to
npm update or
npm uninstall, you can install cargo-edit which enhances Cargo with
cargo rm and
cargo upgrade subcommands.
You can also specify the dependency version using patterns similar 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 or cargo-make
Nodemon is an essential tool for Node.js development - it watches for changes to files and automatically restarts the application. cargo-watch is the equivalent in Rust world.
Thanks for reading! Feel free to follow me in Twitter for more posts like this :)