What we can’t write Node.js without is Npm. It’s like writing Rust without Cargo.
You may not know Rust or Cargo. It doesn’t matter. Let’s learn Cargo from NPM and get you started with Rust.
We’ll start with the familiar node.js:
The process of writing Node.js code
We will create a directory and execute NPM init in that directory
mkdir node-pro
cd node-pro
npm init
Copy the code
The console asks for some information interactively and generates package.json
You can also add -y to create quickly with the default value.
We will then use NPM install XXX to install dependencies, such as Babel
npm install babel
Copy the code
Add Babel to your package.json dependencies
A package-lock.json file is also generated to record the dependent version (yarn.lock file if installed with yarn).
Dependencies are installed under node_modules.
After that, we’ll create SRC and write some code.
Then specify compile, test, and other scripts in package.json:
Then, after the code is written, run the build through NPM run build, and run the test through NPM run test:
npm run build
npm run test
Copy the code
The generated artifacts can be published to the NPM repository via NPM publish.
Other commands may also be used: NPM update to update dependencies, NPM search to search dependencies, and so on.
This is the development flow of a Node.js project, regardless of the specific node.js code written, the entire tool chain is connected by NPM.
You may say that this is very basic, is not the introductory content?
Yes, NPM is the way to get started with Node.js, so Rust is the way to get started with Cargo.
Also, the NPM and Cargo designs are so much like each other that it’s easy to see them first.
So what are we waiting for? Let’s learn Cargo.
The process of writing Rust code
As with the Node.js project, you can create the directory first and then initialize the code with cargo Init. (You can also use cargo New directly, which has the same effect as mkdir + cargo init below.)
mkdir rust-pro
cd rust-pro
cargo init
Copy the code
This directory structure is then created:
Cargo. Toml is equivalent to package.json and also declares package information and dependencies.
Even SRC is now available and Git is initialized, which is much nicer than NPM init (no wonder people like Rust, it’s a very thin tool chain).
Cargo. Toml reads like this:
[package] describes the package and [dependencies] describes the dependencies.
We use cargo Search to search for the next package (equivalent to NPM search) :
The html2MD version is 0.2.13. We put it in the dependency:
Then we’ll write some code to convert HTML to markdown:
Then compile and execute:
cargo build
cargo run
Copy the code
You can see the result of the execution:
What we do with NPM Run Build is also a build command, just a self-configured tripartite build tool, whereas Cargo uses the built-in build tool.
So here we are running the first RUST program. Does the process have a slight resemblance to NPM?
Also, like yarn.lock or package-lock.json, cargo has cargo. Lock to record dependency details:
Cargo test can then be executed to run the test code, and Cargo Publish can be executed to upload to the central repository. This is similar to the overall process of NPM.
Npm is similar to Cargo
Why is Cargo so similar to NPM?
This shows that this is already best practice! Scaffolding init, build build, run, test, publish, and so on into a command tool built into the language’s tool chain.
Take a look at ancient C++ to see the difference:
C++ is compiled using clang or GCC. The rest of the functionality is not there. You need to use cmake to declare some other commands. Most importantly, there is no central repository and dependency management tools. Each dependency has to be manually downloaded and placed in the project directory, which is particularly troublesome.
Since this is a must-have feature, why not build it into the language’s toolchain?
So both NPM and Cargo have init, install, update, build, test, publish built in, as well as support for a central repository and dependency management.
This is a best practice for the modern language tool chain, and will feel the same for any modern language tool.
conclusion
Cargo is to Rust what Npm is to Node.js, both are integrated command-line toolchains for initialization, dependency management, build, publish, and so on.
The development flow of node.js projects is as follows:
- NPM init initializes the project
- NPM Search search dependencies
- NPM install Installs dependencies
- NPM update update dependency
- NPM run build performs the build
- NPM run test Executes the test
- NPM publish publishes to the central repository
The Rust project follows a similar development process:
- Cargo init initializes the project (or cargo new, which is equivalent to mkdir + cargo init)
- Cargo Search search dependencies
- Cargo Install Indicates the installation dependency
- Cargo Update Update dependencies
- Manually fill in the dependencies into Cargo. Toml
- Cargo Build compiles the build code
- Cargo Run runs the code
- Cargo test Runs unit tests
- Cargo Publish publishes to the central warehouse
Although the exact syntax and project structure are different, the flow across the tool chain is similar. This is best practice for modern language toolchains.
In contrast, C++ has no dependency management, no integrated toolchain, and a far less rewarding development experience than rust with Cargo and node.js with NPM.
In fact, we can learn Rust or any other language by comparing it to the familiar JS language, because they use different abstractions of computers and face similar problems, but with different solutions.
You can learn Cargo from Npm