当前位置:网站首页>Rust 入门指南(crate 管理)
Rust 入门指南(crate 管理)
2022-07-28 17:31:00 【王泰】
Rust 入门指南(crate 管理)
本文为「Rust 入门指南」系列的第二部分。 这篇文章将重点介绍 cargo cli 工具和 Rust 的包管理系统,包括:
本文主要介绍 cargo 命令行和 Rust 的包管理系统,包括
搜索 crates 在项目中使用 crates 配置 crate 安装 / 使用 cargo-watch
Rust 的基础知识可以参考上一篇内容 Rust — 入门指南(rustup, cargo)
可以使用 https://geekcode.cloud 云开发环境一键创建 rust 环境快速开始后面的内容。
接下来的内容默认你已经读过上一篇的内容,并在 https://geekcode.cloud 体验了 rustc 和 rustup。
Crates
crates 就是Rust 包。 生成的 crates 注册在 crates.io 仓库上,我们可以在 crates.io 找到全部的 crates。
使用 cargo 命令搜索 crate。 例如,要搜索与 sql 相关的 crates :
$ cargo search sql
search 命令的结果显示与相关的 crate 名称和版本号:

使用 Crates
把需要的 crates 添加到 Cargo.toml 文件中。
使用$ cargo new {project_name} 创建出的 Cargo.toml 内容:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2022"# See more keys and their definitions athttps://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
编辑 Cargo.toml 文件,添加上面搜索到的 sql create:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2018"# See more keys and their definitions athttps://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
sql = "0.4.3" <-- Add dependency here
注意,Cargo 依赖项与 SemVer 语法完全兼容,允许按需选择性更新。
Crates、Git 和 Feature toggles
Cargo 有很强大的功能获得 crates 。可以使用不同的方式指定 crate 。
标准方式:
[dependencies]
bracket-lib = “~0.8.1”
指定 Git 仓库:
[dependencies]
bracket-lib ={ git = "https://github.com/thebracket/bracket-lib" }
使用 feature toggles ,在下面的示例中,使用 bracket-lib ,但禁用默认渲染引擎并使用 vulkan 引擎 -
[dependencies]
bracket-lib = {
git = "https://github.com/thebracket/bracket-lib",
default-features = false,
features = [ "amethyst_engine_vulkan" ]
}
源(Source)和二进制 Crates
C# 或 NPM 工程师,会注意到在添加 crate 后运行$ cargo build 时控制台输出:
Compiling sql v0.4.3
Compiling hello_world v0.1.0 (/Users/gian/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.99s
大多数 Rust crate 都是基于源码的,添加到本地项目时,cargo 会自动获取源代码,然后在平台编译 crate。
如果指定不同的 target 平台,cargo 会重新编译所有 crate。
Cargo 支持安装 binary crates,特定于 crates 是可执行文件。 所以 cargo 获取、编译然后安装生成的可执行文件。
要安装二进制 crate,您可以直接使用 cargo:
$ cargo install {crate_name}
注意:使用 cargo install仅支持二进制 crate。 不能使用此命令来安装/添加常规 crate。
请看下面 cargo-watch 的示例。
Cargo-watch
对于大多数 crate,可以将它们直接添加到 Cargo.toml。 首先使用 cargo search 命令查找最新版本,或直接访问 crates.io,但不能使用 cargo install。
但是,如果安装的 crate 是二进制 crate,那么只能使用 cargo install 即可获取、编译和安装 crate 以供您使用。
注意: 与其他 crate 不同,安装二进制 crate 只需执行一次(除非需要更新二进制 crate 的版本)。
Updating crates.io index
Ignored package `cargo-watch v7.8.1` is already installed, use --force to override
cargo-watch 用于监控项目中的文件变化并运行命令。
安装 cargo-watch :
$ cargo install cargo-watch
安装完成后,可以立即能够访问 cargo-watch 工具:
$ cargo-watch -x build
此命令将启动一个进程来监视项目中文件的更改,在这种情况下,当检测到更改时会自动运行cargo build。
您可以像这样将多个命令链接在一起 -
$ cargo-watch -x fmt -x test
如果您已安装了 Rust linter,但它会触发 linter,然后运行任何每次文件更改时测试!
您可以使用 cargo-watch 做更多事情,因此请查看 cargo-watch crate 文档。
总结
Rust crates 与大多数其他包装系统没有特别的不同,但如上所述,需要注意的事情很少。
cargo-watch crate 是二进制 crate 的例子,也是一个非常方便的实用程序,当您在 VSCode 中进行开发并希望对代码更改等进行即时反馈时。
还有很多有用的 crates 在crates.io 上。
keep rusty!
加入 GeekCode Community 参与定义 Cloud Based IDE

点击【阅读原文】访问 GeekCode
边栏推荐
- Avoidance Adjusted Climbrate
- Ardupilot software in the loop simulation and online debugging
- Application of time series database in monitoring operation and maintenance platform
- 搜索问题与技术
- Qt: one signal binds multiple slots
- pytest 自定义HOOK函数
- sudo rosdep init 出现 ERROR: cannot download default
- Application value of MES production management system to equipment
- [filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
- ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)
猜你喜欢
![[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code](/img/31/e4cd4c261a7fc5cfa731976314530b.png)
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code

From Bayesian filter to Kalman filter (zero)

Qt: one signal binds multiple slots

VAE: understanding and implementation of variational self encoder

三类6种地图可视化软件测评,最好用的工具居然是它

vim学习手册

How many of the top ten test tools in 2022 do you master

RTC clock: a calendar

关于白盒测试,这些技巧你得游刃有余~

CVPR19 - 调参干货《Bag of Tricks for Image Classification with Convolutional Neural Network》
随机推荐
Share several coding code receiving verification code platforms, which will be updated in February 2022
Pytest custom hook function
Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)
R language text mining and natural language processing tutorial
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements
Application value of MES production management system to equipment
RFs self study notes (III): clutter model - first determine the number with Poisson distribution, and then use uniform distribution as probability distribution
力扣 1331. 数组序号转换
Sudo rosdep init error: cannot download default
SaltStack配置管理
Pandownload revival tutorial
Get to know nodejs for the first time (with cases)
【笔记】《结网:互联网产品经理改变世界》
以数字化转型为契机,3C企业如何通过SRM供应商云协同平台实现高效协同?
About ASM redundancy
2022年中总结
Application of time series database in museum environment detection
ES6 new - arrow function
使用SaltStack自动化部署Zabbix