preface

Cargo plays the role of package manager in the rust world, similar to the role of NPM in Node.js. By default cargo will download the required dependency packages from crates. IO, where you can register an account and push packages, similar to what you can do at npmjs.com

Corresponding relations between

Project profile

In Node.js we use package.json as a configuration file, while in Rust we use Cargo. Cargo. Toml uses THE TOML format instead of JSON. The Cargo. Toml file is used to tell Cargo which modules The current project depends on, how to run tests, and how to build your project

Initialize the new project

In Node.js we use the NPM init command for initialization, and in Rust we use cargo init and cargo new to do the same thing. Cargo Init will initialize in the current folder, cargo New will specify a folder

Install dependencies

In Node.js we use NPM install [dep] to install dependent modules. In Rust we can add dependencies using cargo add [dep] if cargo edit is installed.

cargo install cargo-edit
Copy the code

After the installation, four subcommands are added: add, rm, upgrade, and set-version

Global installation

Analogous to NPM install –global in Node.js, we use cargo install in Rust

Run the test

Analogous to NPM test in Node.js, we use cargo test in Rust

Publish module

Analogous to NPM publish in Node.js, we use cargo publish in Rust

Run the task

In Node.js we use NPM run XXX to run tasks, whereas in Rust, apart from a few common commands, the rest is up to the user. For example, we can run a code through Cargo Run, or use Cargo Bench to analyze the performance of a piece of code, or use Cargo Build for packaging, or cargo Clean to clear the packaging directory (target by default), Or you can use Cargo Doc to generate documentation. Cargo also supports the Build Scripts mechanism to ensure that specified programs can be run before packaging.

Makefiles are no longer needed in JavaScript, but they are not so lucky in Rust, where they are still common. However, just is gaining popularity. It compensates for some of the weaknesses of Makefiles and is syntactically similar. You can install it as follows:

$ cargo install just
Copy the code

Cargo -make and cargo- CMD are also good alternatives

The workspace and monorepo

In Rust, you can create a Cargo. Toml file in the root directory as an entry to the workspace. It describes the contents contained in the workspace, roughly as follows:

[workspace]
members = [
  "crates/*"
]
Copy the code

Modules that reference each other in a workspace can point to local folders as dependencies:

[dependencies]
other-project = { path = ".. /other-project" }
Copy the code

Extra tools

cargo-edit

It has been introduced above and will not be repeated

cargo-workspaces

Cargo WorkSpaces (or Cargo WS) simplifies the creation and management of workspaces. Inspired by Lerna of Node.js, the greatest value is in automatically publishing modules in Workspaces and replacing local dependencies. You can install it in the following ways:

$ cargo install cargo-workspaces
Copy the code

cargo-expand

Macros are so common in Rust that even your first Hello Word program had to use them. While macros can help you cut down on redundant code but make debugging more difficult, cargo expand helps reduce the difficulty encountered during debugging. Cargo -expand depends on the Nightly toolchain:

rustup install nightly
Copy the code

Then install cargo-expand:

$ cargo install cargo-expand
Copy the code

Once installed, you can print out the full source code using Cargo Expand [item]

Note: Cargo Expand accepts a name, not a file path. Instead of expanding SRC /main.rs, the cargo expand main command expands the main() function in the project’s root directory. Cargo expand some_module:: Another Normally, expand modules in SRC /some_module/another to run cargo expand some_module::another. Don’t worry, we’ll get into this later.

For example, executing cargo new generates a SRC /main.rs file:

fn main() {
  println!("Hello, world!");
}
Copy the code

The println! () is a macro, and we run cargo expand to see what results are generated:

fn main() {
    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(
            &["Hello, world! \n"],
            &match() {() => [],},); }; }Copy the code

tomlq

This is not a cargo XX command and is useful when querying data in.toml files

An overview of the

The contrast from NPM to cargo is clear when you add cargo edit. How do you configure the RUST development environment in Visual Studio Code