"Yiya Neng Pao Jiu Xing" is my personal solo podcast, a draft version of a web worker podcast. It focuses on casual and fast consumption. If you are interested in the topics I talk about and want to have a serious discussion in the web worker podcast, feel free to interact with me.
This episode is a speed pass of the Syntax.fm 647 Rust for JavaScript Developers - Node vs Rust Concepts released on 20230731. The original duration is about 30 minutes, and the speed pass is about 13 minutes.
When comparing the ecosystem solutions of JavaScript (JS) and Rust, we can introduce them at a basic level without the need for Rust usage experience.
Package Manager#
Package management tools:
- Node can use
npm
/pnpm
/yarn
- Rust uses Cargo
Cargo can not only manage packages but also run tests, unlike vitest/jest/mocha/chai in Node. It can also build your documentation, publish Rust libraries, and run code similar to npm run start
.
The main package file Cargo.toml
is similar to package.json
.
About Cargo.toml
:
Cargo.toml is a configuration file in the Rust language used to describe and manage project metadata and dependencies.
In a Rust project, the
Cargo.toml
file is located in the root directory of the project. It uses the TOML (Tom's Obvious, Minimal Language) format to describe the project's configuration information.TOML (Tom's Obvious, Minimal Language) is a lightweight format for configuration files created by Tom Preston-Werner. Its design goal is to be easy to read and write while maintaining a concise and clear syntax.
TOML is designed to make configuration files easy to write and read while maintaining a concise and clear syntax. It has been widely used in the Rust community and has become one of the commonly used configuration file formats in Rust projects.
Similar to YAML.
Cargo build for building. It can be published to an internal registry in Rust, similar to npm's registry: crates.io.
Node has different options, Python has more, but Rust only has one. There is a Rust fork called crab.
Cargo doc
:
cargo doc
is a command-line tool in the Rust language used to generate documentation for a project. It generates an HTML-formatted documentation website based on comments and doc comments in the project, allowing developers to conveniently view and browse the project's documentation.
It extracts documentation from code comments. Continuing to look at crates.io, every project has a readme, and most of them are clear. Generating documentation is easy.
Reading docs.rs can not only learn Rust but also learn this style, which can also be beneficial in JavaScript.
Similar to JSDoc, but easier than JSDoc, although it requires learning the syntax. practice.rs's comments-docs
Rust is fully typed, similar to TypeScript's static typing, and also has generics. Experience in TypeScript can be applied to Rust.
Continuing to look at memory safety, it is a complex topic. JavaScript has garbage collection, while Rust builds memory safety on top of no garbage collection. The details are complex, so let's skip them.
Concurrency, in JavaScript, is single-threaded. Rust supports multi-threading.
Promise/async-await in Rust is called a feature. Rust has async-await in a single thread, similar to JavaScript.
The position where the await
keyword appears is different from JavaScript.
Sometimes when looking at Rust, you can see a lot of JavaScript-like code.
Rust does not have try..catch, and exception handling is similar to pattern matching, somewhat like switch.
In summary, the error handling in Rust uses pattern matching to handle errors, which is safer and more reliable. Compared to the traditional try-catch mechanism, it can capture potential errors at compile time and provide better code readability and maintainability.
Importing. The import syntax is different. In Rust, modules are imported first before using specific methods, similar to Python. This kind of inference using double colons and slashes is ignored.
In JavaScript, everything is an object, so we can access corresponding data and functionality with a.b.c
. In Rust, ::
is used.
In a high-level description, what is different?
- More joyful technology choices, using what Rust provides is enough, and many things don't need to be chosen.
- Rust sometimes resembles TypeScript/JavaScript.
There are 13 basic types in Rust, including a large number of integer types. There are two different flow types. We can continue to discuss them later, such as types, structures, and variables.