辛宝Otto

辛宝Otto 的玄酒清谈

北漂前端程序员儿 / 探索新事物 / Web Worker 主播之一/内向话痨
xiaoyuzhou
email

09-Speedpass Syntax 647 Comparison of Node/Rust Basic Concepts

"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, and 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 time, I bring you a speed pass reading of Syntax.fm 647 Rust for JavaScript Developers - Node vs Rust Concepts, which was released on July 31, 2023. The original duration was 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 from a basic level without the need for Rust usage experience.

Package Manager:

  • Node can use npm/pnpm/yarn
  • Rust uses Cargo

Cargo not only manages packages but also runs 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 is a lightweight format for configuration files created by Tom Preston-Werner. It is designed to be easy to read and write while maintaining a concise and clear syntax.
  • TOML aims 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:

  • Builds the project.
  • Can be published to an internal registry in Rust, similar to npm.
  • There is only one option in Rust, unlike Node which has different options. 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 projects. It generates an HTML-formatted documentation website based on comments and doc comments in the project, allowing developers to easily view and browse the project's documentation.

Documentation is extracted 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 the docs.rs documentation not only allows you to 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 with memory safety, it is a complex topic. JavaScript has garbage collection, while Rust builds memory safety on top of that. The details are complex and will be skipped.

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 you can see a lot of JavaScript-like code in Rust.

Rust does not have try..catch, and exception handling is done through pattern matching, somewhat similar to switch.

In summary, error handling in Rust uses pattern matching, 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 cannot be used in JavaScript. The specific usage of 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, double colons :: are used.

High-level description:
What is different?

  • The joy of technical choices, using what Rust provides is enough, and many things do not need to be chosen.
  • Rust sometimes resembles TypeScript/JavaScript.

There are 13 basic types, including a large number of integer types. There are two different flow types. We can continue to talk about them later, such as types, structures, and variables.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.