Rust for JavaScript Developers - Tooling Ecosystem Overview

栏目: IT技术 · 发布时间: 5年前

内容简介:This is the first part in a series about introducing the Rust language to JavaScript developers.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;

This is the first part in a series about introducing the Rust language to JavaScript developers.

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:

Tool JavaScript Rust
Version Manager nvm rustup
Package Manager npm Cargo
Package Registry npmjs.com crates.io
Package Manifest package.json Cargo.toml
Dependency Lockfile package-lock.json Cargo.lock
Task Runner npm scripts, gulp etc make, cargo-make
Live Reload nodemon cargo-watch
Linter ESLint, TSLint, JSLint Clippy
Formatter Prettier rustfmt
Dependency Vulnerability Checker npm audit cargo-audit

Setup

Rust is installed using the rustup command. rustup is similar to nvm in Node.js. You can use it to install and manage multiple versions of Rust and more.

Cargo

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.

Rust’s packages are called “crates”, and they’re downloaded from the crates.io registry similar to how NPM packages are downloaded from npmjs.com .

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!

Project Setup

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

Cargo.toml

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]

The [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 .

Dependency Management

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"

and run

$ 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 install , npm update or npm uninstall , you can install cargo-edit which enhances Cargo with cargo add , cargo rm and cargo upgrade subcommands.

You can also specify the dependency version using patterns similar to NPM.

Development tools

Task Runner

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

Live Reload

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.

Linter and Formatter

Rust has builtin linter called Clippy and formatter called rustfmt . They’re equivalent to ESLint and Prettier in JS ecosystem. Precommit hooks can be managed using cargo-husky .

Vulnerability Checking

Scanning for vulnerabilities in dependencies is done using cargo-audit and is very similar to npm audit .

Thanks for reading! :)


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Perl入门经典

Perl入门经典

[美]Curtis "Ovid" Poe / 朱允刚、韩雷、叶斌 / 清华大学出版社 / 2013-9-20 / 78.00

作为最有影响力的编程语言之一,Perl被广泛用在Web开发、数据处理和系统管理中。无论是Perl新手,还是想要加强自己实战技能的Perl程序员,《Perl入门经典》都提供了处理日常情况所需的各种技术。凭借十多年的Perl经验,作者Curtis“Ovid”Poe一开始先简单回顾了Perl的基础知识,然后以此为出发点,举例说明了Perl在工作场所中的各种真实用法。此外,书中还包含了一些动手练习、宝贵建......一起来看看 《Perl入门经典》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具