From be4f7e72813c1335bbee428b6b33b3be49ed6591 Mon Sep 17 00:00:00 2001 From: KaiserY Date: Wed, 9 Feb 2022 19:16:45 +0800 Subject: [PATCH] update to ch14-05 --- README.md | 4 +- src/ch14-01-release-profiles.md | 10 +- src/ch14-02-publishing-to-crates-io.md | 134 ++++++----------------- src/ch14-03-cargo-workspaces.md | 142 +++++++++++-------------- src/ch14-04-installing-binaries.md | 17 +-- 5 files changed, 109 insertions(+), 198 deletions(-) diff --git a/README.md b/README.md index 1f7323e..a07aa62 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# Rust 程序设计语言(第二版 & 2018 edition) 简体中文版 +# Rust 程序设计语言(2021 edition 施工中) 简体中文版 ![Build Status](https://github.com/KaiserY/trpl-zh-cn/workflows/CI/badge.svg) ## 状态 -2018 edition 的翻译迁移已基本完成,欢迎阅读学习! +2021 edition 施工中。 PS: diff --git a/src/ch14-01-release-profiles.md b/src/ch14-01-release-profiles.md index 73fc0db..69e14eb 100644 --- a/src/ch14-01-release-profiles.md +++ b/src/ch14-01-release-profiles.md @@ -2,7 +2,7 @@ > [ch14-01-release-profiles.md](https://github.com/rust-lang/book/blob/main/src/ch14-01-release-profiles.md) >
-> commit 0f10093ac5fbd57feb2352e08ee6d3efd66f887c +> commit d44317c3122b44fb713aba66cc295dee3453b24b 在 Rust 中 **发布配置**(*release profiles*)是预定义的、可定制的带有不同选项的配置,他们允许程序员更灵活地控制代码编译的多种选项。每一个配置都彼此相互独立。 @@ -10,11 +10,11 @@ Cargo 有两个主要的配置:运行 `cargo build` 时采用的 `dev` 配置 这些配置名称可能很眼熟,因为它们出现在构建的输出中: -```text +```console $ cargo build - Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs + Finished dev [unoptimized + debuginfo] target(s) in 0.0s $ cargo build --release - Finished release [optimized] target(s) in 0.0 secs + Finished release [optimized] target(s) in 0.0s ``` 构建输出中的 `dev` 和 `release` 表明编译器在使用不同的配置。 @@ -44,4 +44,4 @@ opt-level = 1 这会覆盖默认的设置 `0`。现在运行 `cargo build` 时,Cargo 将会使用 `dev` 的默认配置加上定制的 `opt-level`。因为 `opt-level` 设置为 `1`,Cargo 会比默认进行更多的优化,但是没有发布构建那么多。 -对于每个配置的设置和其默认值的完整列表,请查看 [Cargo 的文档](https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections)。 +对于每个配置的设置和其默认值的完整列表,请查看 [Cargo 的文档](https://doc.rust-lang.org/cargo/reference/profiles.html)。 diff --git a/src/ch14-02-publishing-to-crates-io.md b/src/ch14-02-publishing-to-crates-io.md index d5e18a1..cadc773 100644 --- a/src/ch14-02-publishing-to-crates-io.md +++ b/src/ch14-02-publishing-to-crates-io.md @@ -1,7 +1,7 @@ ## 将 crate 发布到 Crates.io > [ch14-02-publishing-to-crates-io.md](https://github.com/rust-lang/book/blob/main/src/ch14-02-publishing-to-crates-io.md)
-> commit c084bdd9ee328e7e774df19882ccc139532e53d8 +> commit 7ddc28cfe0bfa6c531a6475c7fa41dfa66e8943c 我们曾经在项目中使用 [crates.io](https://crates.io) 上的包作为依赖,不过你也可以通过发布自己的包来向他人分享代码。[crates.io](https://crates.io) 用来分发包的源代码,所以它主要托管开源代码。 @@ -11,25 +11,13 @@ Rust 和 Cargo 有一些帮助他人更方便找到和使用你发布的包的 准确的包文档有助于其他用户理解如何以及何时使用他们,所以花一些时间编写文档是值得的。第三章中我们讨论了如何使用两斜杠 `//` 注释 Rust 代码。Rust 也有特定的用于文档的注释类型,通常被称为 **文档注释**(_documentation comments_),他们会生成 HTML 文档。这些 HTML 展示公有 API 文档注释的内容,他们意在让对库感兴趣的程序员理解如何 **使用** 这个 crate,而不是它是如何被 **实现** 的。 -文档注释使用三斜杠 `///` 而不是两斜杆以支持 Markdown 注解来格式化文本。文档注释就位于需要文档的项的之前。示例 14-1 展示了一个 `my_crate` crate 中 `add_one` 函数的文档注释: +文档注释使用三斜杠 `///` 而不是两斜杆以支持 Markdown 注解来格式化文本。文档注释就位于需要文档的项的之前。示例 14-1 展示了一个 `my_crate` crate 中 `add_one` 函数的文档注释, 文件名: src/lib.rs -````rust,ignore -/// Adds one to the number given. -/// -/// # Examples -/// -/// ``` -/// let arg = 5; -/// let answer = my_crate::add_one(arg); -/// -/// assert_eq!(6, answer); -/// ``` -pub fn add_one(x: i32) -> i32 { - x + 1 -} -```` +```rust,ignore +{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-01/src/lib.rs}} +``` 示例 14-1:一个函数的文档注释 @@ -61,7 +49,7 @@ pub fn add_one(x: i32) -> i32 { running 1 test test src/lib.rs - add_one (line 5) ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.27s ``` 现在尝试改变函数或例子来使例子中的 `assert_eq!` 产生 panic。再次运行 `cargo test`,你将会看到文档测试捕获到了例子与代码不再同步! @@ -75,13 +63,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out 文件名: src/lib.rs ```rust,ignore -//! # My Crate -//! -//! `my_crate` is a collection of utilities to make performing certain -//! calculations more convenient. - -/// Adds one to the number given. -// --snip-- +{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-02/src/lib.rs:here}} ``` 示例 14-2:`my_crate` crate 整体的文档 @@ -108,38 +90,8 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out 文件名: src/lib.rs -```rust,ignore -//! # Art -//! -//! A library for modeling artistic concepts. - -pub mod kinds { - /// The primary colors according to the RYB color model. - pub enum PrimaryColor { - Red, - Yellow, - Blue, - } - - /// The secondary colors according to the RYB color model. - pub enum SecondaryColor { - Orange, - Green, - Purple, - } -} - -pub mod utils { - use crate::kinds::*; - - /// Combines two primary colors in equal amounts to create - /// a secondary color. - pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor { - // --snip-- -# SecondaryColor::Orange - } -} -# fn main() {} +```rust,noplayground,test_harness +{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-03/src/lib.rs:here}} ``` 示例 14-3:一个库 `art` 其组织包含 `kinds` 和 `utils` 模块 @@ -157,14 +109,7 @@ pub mod utils { 文件名: src/main.rs ```rust,ignore -use art::kinds::PrimaryColor; -use art::utils::mix; - -fn main() { - let red = PrimaryColor::Red; - let yellow = PrimaryColor::Yellow; - mix(red, yellow); -} +{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-04/src/main.rs}} ``` 示例 14-4:一个通过导出内部结构使用 `art` crate 中项的 crate @@ -176,21 +121,7 @@ fn main() { 文件名: src/lib.rs ```rust,ignore -//! # Art -//! -//! A library for modeling artistic concepts. - -pub use self::kinds::PrimaryColor; -pub use self::kinds::SecondaryColor; -pub use self::utils::mix; - -pub mod kinds { - // --snip-- -} - -pub mod utils { - // --snip-- -} +{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-05/src/lib.rs:here}} ``` 示例 14-5:增加 `pub use` 语句重导出项 @@ -206,12 +137,7 @@ pub mod utils { 文件名: src/main.rs ```rust,ignore -use art::PrimaryColor; -use art::mix; - -fn main() { - // --snip-- -} +{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-06/src/main.rs:here}} ``` 示例 14-6:一个使用 `art` crate 中重导出项的程序 @@ -224,7 +150,7 @@ fn main() { 在你可以发布任何 crate 之前,需要在 [crates.io](https://crates.io) 上注册账号并获取一个 API token。为此,访问位于 [crates.io](https://crates.io) 的首页并使用 GitHub 账号登陆。(目前 GitHub 账号是必须的,不过将来该网站可能会支持其他创建账号的方法)一旦登陆之后,查看位于 [https://crates.io/me/](https://crates.io/me/) 的账户设置页面并获取 API token。接着使用该 API token 运行 `cargo login` 命令,像这样: -```text +```console $ cargo login abcdefghijklmnopqrstuvwxyz012345 ``` @@ -245,13 +171,16 @@ name = "guessing_game" 即使你选择了一个唯一的名称,如果此时尝试运行 `cargo publish` 发布该 crate 的话,会得到一个警告接着是一个错误: -```text +```console $ cargo publish - Updating registry `https://github.com/rust-lang/crates.io-index` -warning: manifest has no description, license, license-file, documentation, -homepage or repository. + Updating crates.io index +warning: manifest has no description, license, license-file, documentation, homepage or repository. +See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info. --snip-- -error: api errors: missing or empty metadata fields: description, license. +error: failed to publish to registry at https://crates.io + +Caused by: + the remote server responded with an error: missing or empty metadata fields: description, license. Please see https://doc.rust-lang.org/cargo/reference/manifest.html for how to upload metadata ``` 这是因为我们缺少一些关键信息:关于该 crate 用途的描述和用户可能在何种条款下使用该 crate 的 license。为了修正这个错误,需要在 _Cargo.toml_ 中引入这些信息。 @@ -280,8 +209,7 @@ license = "MIT" [package] name = "guessing_game" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" description = "A fun game where you guess what number the computer has chosen." license = "MIT OR Apache-2.0" @@ -298,15 +226,15 @@ license = "MIT OR Apache-2.0" 再次运行 `cargo publish` 命令。这次它应该会成功: -```text +```console $ cargo publish - Updating registry `https://github.com/rust-lang/crates.io-index` -Packaging guessing_game v0.1.0 (file:///projects/guessing_game) -Verifying guessing_game v0.1.0 (file:///projects/guessing_game) -Compiling guessing_game v0.1.0 + Updating crates.io index + Packaging guessing_game v0.1.0 (file:///projects/guessing_game) + Verifying guessing_game v0.1.0 (file:///projects/guessing_game) + Compiling guessing_game v0.1.0 (file:///projects/guessing_game/target/package/guessing_game-0.1.0) - Finished dev [unoptimized + debuginfo] target(s) in 0.19 secs -Uploading guessing_game v0.1.0 (file:///projects/guessing_game) + Finished dev [unoptimized + debuginfo] target(s) in 0.19s + Uploading guessing_game v0.1.0 (file:///projects/guessing_game) ``` 恭喜!你现在向 Rust 社区分享了代码,而且任何人都可以轻松的将你的 crate 加入他们项目的依赖。 @@ -325,13 +253,13 @@ Uploading guessing_game v0.1.0 (file:///projects/guessing_game) 为了撤回一个 crate,运行 `cargo yank` 并指定希望撤回的版本: -```text +```console $ cargo yank --vers 1.0.1 ``` 也可以撤销撤回操作,并允许项目可以再次开始依赖某个版本,通过在命令上增加 `--undo`: -```text +```console $ cargo yank --vers 1.0.1 --undo ``` diff --git a/src/ch14-03-cargo-workspaces.md b/src/ch14-03-cargo-workspaces.md index 4c768d7..14f83c1 100644 --- a/src/ch14-03-cargo-workspaces.md +++ b/src/ch14-03-cargo-workspaces.md @@ -2,7 +2,7 @@ > [ch14-03-cargo-workspaces.md](https://github.com/rust-lang/book/blob/main/src/ch14-03-cargo-workspaces.md) >
-> commit 6d3e76820418f2d2bb203233c61d90390b5690f1 +> commit 7ddc28cfe0bfa6c531a6475c7fa41dfa66e8943c 第十二章中,我们构建一个包含二进制 crate 和库 crate 的包。你可能会发现,随着项目开发的深入,库 crate 持续增大,而你希望将其进一步拆分成多个库 crate。对于这种情况,Cargo 提供了一个叫 **工作空间**(*workspaces*)的功能,它可以帮助我们管理多个相关的协同开发的包。 @@ -10,7 +10,7 @@ **工作空间** 是一系列共享同样的 *Cargo.lock* 和输出目录的包。让我们使用工作空间创建一个项目 —— 这里采用常见的代码以便可以关注工作空间的结构。有多种组织工作空间的方式;我们将展示一个常用方法。我们的工作空间有一个二进制项目和两个库。二进制项目会提供主要功能,并会依赖另两个库。一个库会提供 `add_one` 方法而第二个会提供 `add_two` 方法。这三个 crate 将会是相同工作空间的一部分。让我们以新建工作空间目录开始: -```text +```console $ mkdir add $ cd add ``` @@ -20,18 +20,14 @@ $ cd add 文件名: Cargo.toml ```toml -[workspace] - -members = [ - "adder", -] +{{#include ../listings/ch14-more-about-cargo/no-listing-01-workspace-with-adder-crate/add/Cargo.toml}} ``` 接下来,在 *add* 目录运行 `cargo new` 新建 `adder` 二进制 crate: -```text +```console $ cargo new adder - Created binary (application) `adder` project + Created binary (application) `adder` package ``` 到此为止,可以运行 `cargo build` 来构建工作空间。*add* 目录中的文件应该看起来像这样: @@ -48,26 +44,21 @@ $ cargo new adder 工作空间在顶级目录有一个 *target* 目录;`adder` 并没有自己的 *target* 目录。即使进入 *adder* 目录运行 `cargo build`,构建结果也位于 *add/target* 而不是 *add/adder/target*。工作空间中的 crate 之间相互依赖。如果每个 crate 有其自己的 *target* 目录,为了在自己的 *target* 目录中生成构建结果,工作空间中的每一个 crate 都不得不相互重新编译其他 crate。通过共享一个 *target* 目录,工作空间可以避免其他 crate 多余的重复构建。 -### 在工作空间中创建第二个 crate +### 在工作空间中创建第二个包 接下来,让我们在工作空间中指定另一个成员 crate。这个 crate 位于 *add-one* 目录中,所以修改顶级 *Cargo.toml* 为也包含 *add-one* 路径: 文件名: Cargo.toml ```toml -[workspace] - -members = [ - "adder", - "add-one", -] +{{#include ../listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/Cargo.toml}} ``` 接着新生成一个叫做 `add-one` 的库: -```text -$ cargo new add-one --lib - Created library `add-one` project +```console +$ cargo new add_one --lib + Created library `add_one` package ``` 现在 *add* 目录应该有如下目录和文件: @@ -75,7 +66,7 @@ $ cargo new add-one --lib ```text ├── Cargo.lock ├── Cargo.toml -├── add-one +├── add_one │ ├── Cargo.toml │ └── src │ └── lib.rs @@ -90,10 +81,8 @@ $ cargo new add-one --lib 文件名: add-one/src/lib.rs -```rust -pub fn add_one(x: i32) -> i32 { - x + 1 -} +```rust,noplayground +{{#rustdoc_include ../listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/add_one/src/lib.rs}} ``` 现在工作空间中有了一个库 crate,让 `adder` 依赖库 crate `add-one`。首先需要在 *adder/Cargo.toml* 文件中增加 `add-one` 作为路径依赖: @@ -101,9 +90,7 @@ pub fn add_one(x: i32) -> i32 { 文件名: adder/Cargo.toml ```toml -[dependencies] - -add-one = { path = "../add-one" } +{{#include ../listings/ch14-more-about-cargo/no-listing-02-workspace-with-two-crates/add/adder/Cargo.toml:6:7}} ``` cargo并不假定工作空间中的Crates会相互依赖,所以需要明确表明工作空间中 crate 的依赖关系。 @@ -113,30 +100,25 @@ cargo并不假定工作空间中的Crates会相互依赖,所以需要明确表 文件名: adder/src/main.rs ```rust,ignore -use add_one; - -fn main() { - let num = 10; - println!("Hello, world! {} plus one is {}!", num, add_one::add_one(num)); -} +{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs}} ``` 示例 14-7:在 `adder` crate 中使用 `add-one` 库 crate 在 *add* 目录中运行 `cargo build` 来构建工作空间! -```text +```console $ cargo build - Compiling add-one v0.1.0 (file:///projects/add/add-one) + Compiling add_one v0.1.0 (file:///projects/add/add_one) Compiling adder v0.1.0 (file:///projects/add/adder) - Finished dev [unoptimized + debuginfo] target(s) in 0.68 secs + Finished dev [unoptimized + debuginfo] target(s) in 0.68s ``` -为了在顶层 *add* 目录运行二进制 crate,需要通过 `-p` 参数和包名称来运行 `cargo run` 指定工作空间中我们希望使用的包: +为了在顶层 *add* 目录运行二进制 crate,可以通过 `-p` 参数和包名称来运行 `cargo run` 指定工作空间中我们希望使用的包: -```text +```console $ cargo run -p adder - Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs + Finished dev [unoptimized + debuginfo] target(s) in 0.0s Running `target/debug/adder` Hello, world! 10 plus one is 11! ``` @@ -144,40 +126,50 @@ Hello, world! 10 plus one is 11! 这会运行 *adder/src/main.rs* 中的代码,其依赖 `add-one` crate -#### 在工作空间中依赖外部 crate +#### 在工作空间中依赖外部包 还需注意的是工作空间只在根目录有一个 *Cargo.lock*,而不是在每一个 crate 目录都有 *Cargo.lock*。这确保了所有的 crate 都使用完全相同版本的依赖。如果在 *Cargo.toml* 和 *add-one/Cargo.toml* 中都增加 `rand` crate,则 Cargo 会将其都解析为同一版本并记录到唯一的 *Cargo.lock* 中。使得工作空间中的所有 crate 都使用相同的依赖意味着其中的 crate 都是相互兼容的。让我们在 *add-one/Cargo.toml* 中的 `[dependencies]` 部分增加 `rand` crate 以便能够在 `add-one` crate 中使用 `rand` crate: 文件名: add-one/Cargo.toml ```toml -[dependencies] -rand = "0.5.5" +{{#include ../listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add_one/Cargo.toml:6:7}} ``` 现在就可以在 *add-one/src/lib.rs* 中增加 `use rand;` 了,接着在 *add* 目录运行 `cargo build` 构建整个工作空间就会引入并编译 `rand` crate: -```text +```console $ cargo build Updating crates.io index - Downloaded rand v0.5.5 + Downloaded rand v0.8.3 --snip-- - Compiling rand v0.5.5 - Compiling add-one v0.1.0 (file:///projects/add/add-one) + Compiling rand v0.8.3 + Compiling add_one v0.1.0 (file:///projects/add/add_one) +warning: unused import: `rand` + --> add_one/src/lib.rs:1:5 + | +1 | use rand; + | ^^^^ + | + = note: `#[warn(unused_imports)]` on by default + +warning: 1 warning emitted + Compiling adder v0.1.0 (file:///projects/add/adder) - Finished dev [unoptimized + debuginfo] target(s) in 10.18 secs + Finished dev [unoptimized + debuginfo] target(s) in 10.18s ``` 现在顶级的 *Cargo.lock* 包含了 `add-one` 的 `rand` 依赖的信息。然而,即使 `rand` 被用于工作空间的某处,也不能在其他 crate 中使用它,除非也在他们的 *Cargo.toml* 中加入 `rand`。例如,如果在顶级的 `adder` crate 的 *adder/src/main.rs* 中增加 `use rand;`,会得到一个错误: -```text +```console $ cargo build + --snip-- Compiling adder v0.1.0 (file:///projects/add/adder) -error: use of unstable library feature 'rand': use `rand` from crates.io (see -issue #27703) - --> adder/src/main.rs:1:1 +error[E0432]: unresolved import `rand` + --> adder/src/main.rs:2:5 | -1 | use rand; +2 | use rand; + | ^^^^ no external crate `rand` ``` 为了修复这个错误,修改顶级 `adder` crate 的 *Cargo.toml* 来表明 `rand` 也是这个 crate 的依赖。构建 `adder` crate 会将 `rand` 加入到 *Cargo.lock* 中 `adder` 的依赖列表中,但是这并不会下载 `rand` 的额外拷贝。Cargo 确保了工作空间中任何使用 `rand` 的 crate 都采用相同的版本。在整个工作空间中使用相同版本的 `rand` 节省了空间,因为这样就无需多个拷贝并确保了工作空间中的 crate 将是相互兼容的。 @@ -188,74 +180,62 @@ issue #27703) 文件名: add-one/src/lib.rs -```rust -pub fn add_one(x: i32) -> i32 { - x + 1 -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - assert_eq!(3, add_one(2)); - } -} +```rust,noplayground +{{#rustdoc_include ../listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add_one/src/lib.rs}} ``` 在顶级 *add* 目录运行 `cargo test`: -```text +```console $ cargo test - Compiling add-one v0.1.0 (file:///projects/add/add-one) + Compiling add_one v0.1.0 (file:///projects/add/add_one) Compiling adder v0.1.0 (file:///projects/add/adder) - Finished dev [unoptimized + debuginfo] target(s) in 0.27 secs + Finished test [unoptimized + debuginfo] target(s) in 0.27s Running target/debug/deps/add_one-f0253159197f7841 running 1 test test tests::it_works ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - Running target/debug/deps/adder-f88af9d2cc175a5e + Running target/debug/deps/adder-49979ff40686fa8e running 0 tests -test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - Doc-tests add-one + Doc-tests add_one running 0 tests -test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s ``` 输出的第一部分显示 `add-one` crate 的 `it_works` 测试通过了。下一个部分显示 `adder` crate 中找到了 0 个测试,最后一部分显示 `add-one` crate 中有 0 个文档测试。在像这样的工作空间结构中运行 `cargo test` 会运行工作空间中所有 crate 的测试。 也可以选择运行工作空间中特定 crate 的测试,通过在根目录使用 `-p` 参数并指定希望测试的 crate 名称: -```text -$ cargo test -p add-one - Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs +```console +$ cargo test -p add_one + Finished test [unoptimized + debuginfo] target(s) in 0.00s Running target/debug/deps/add_one-b3235fea9a156f74 running 1 test test tests::it_works ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - Doc-tests add-one + Doc-tests add_one running 0 tests -test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s ``` 输出显示了 `cargo test` 只运行了 `add-one` crate 的测试而没有运行 `adder` crate 的测试。 如果你选择向 [crates.io](https://crates.io/)发布工作空间中的 crate,每一个工作空间中的 crate 需要单独发布。`cargo publish` 命令并没有 `--all` 或者 `-p` 参数,所以必须进入每一个 crate 的目录并运行 `cargo publish` 来发布工作空间中的每一个 crate。 -现在尝试以类似 `add-one` crate 的方式向工作空间增加 `add-two` crate 来作为更多的练习! +现在尝试以类似 `add-one` crate 的方式向工作空间增加 `add_two` crate 来作为更多的练习! 随着项目增长,考虑使用工作空间:每一个更小的组件比一大块代码要容易理解。如果它们经常需要同时被修改的话,将 crate 保持在工作空间中更易于协调他们的改变。 diff --git a/src/ch14-04-installing-binaries.md b/src/ch14-04-installing-binaries.md index cf21866..7ea3ba4 100644 --- a/src/ch14-04-installing-binaries.md +++ b/src/ch14-04-installing-binaries.md @@ -2,7 +2,7 @@ > [ch14-04-installing-binaries.md](https://github.com/rust-lang/book/blob/main/src/ch14-04-installing-binaries.md) >
-> commit c084bdd9ee328e7e774df19882ccc139532e53d8 +> commit 359895c6b2e440275a663ee1a3c17e6a94fdc62b `cargo install` 命令用于在本地安装和使用二进制 crate。它并不打算替换系统中的包;它意在作为一个方便 Rust 开发者们安装其他人已经在 [crates.io](https://crates.io/) 上共享的工具的手段。只有拥有二进制目标文件的包能够被安装。**二进制目标** 文件是在 crate 有 *src/main.rs* 或者其他指定为二进制文件时所创建的可执行程序,这不同于自身不能执行但适合包含在其他程序中的库目标文件。通常 crate 的 *README* 文件中有该 crate 是库、二进制目标还是两者都是的信息。 @@ -10,14 +10,17 @@ 例如,第十二章提到的叫做 `ripgrep` 的用于搜索文件的 `grep` 的 Rust 实现。如果想要安装 `ripgrep`,可以运行如下: -```text +```console $ cargo install ripgrep -Updating registry `https://github.com/rust-lang/crates.io-index` - Downloading ripgrep v0.3.2 - --snip-- - Compiling ripgrep v0.3.2 - Finished release [optimized + debuginfo] target(s) in 97.91 secs + Updating crates.io index + Downloaded ripgrep v11.0.2 + Downloaded 1 crate (243.3 KB) in 0.88s + Installing ripgrep v11.0.2 +--snip-- + Compiling ripgrep v11.0.2 + Finished release [optimized + debuginfo] target(s) in 3m 10s Installing ~/.cargo/bin/rg + Installed package `ripgrep v11.0.2` (executable `rg`) ``` 最后一行输出展示了安装的二进制文件的位置和名称,在这里 `ripgrep` 被命名为 `rg`。只要你像上面提到的那样将安装目录加入 `$PATH`,就可以运行 `rg --help` 并开始使用一个更快更 Rust 的工具来搜索文件了!