From 559dd7483967959ab044fdcdae5b14e45f174c19 Mon Sep 17 00:00:00 2001 From: kazeno Date: Tue, 13 May 2025 18:31:23 +0800 Subject: [PATCH] wip: 2024 edition --- .github/workflows/main.yml | 4 ++-- src/ch17-00-async-await.md | 5 ++--- src/ch17-01-futures-and-syntax.md | 5 ++--- src/ch17-02-concurrency-with-async.md | 5 ++--- src/ch17-03-more-futures.md | 5 ++--- src/ch17-04-streams.md | 5 ++--- src/ch17-05-traits-for-async.md | 5 ++--- src/ch17-06-futures-tasks-threads.md | 5 ++--- src/ch20-01-unsafe-rust.md | 5 ++--- src/ch20-02-advanced-traits.md | 5 ++--- src/ch20-03-advanced-types.md | 5 ++--- src/ch20-04-advanced-functions-and-closures.md | 5 ++--- src/ch20-05-macros.md | 5 ++--- src/ch21-00-final-project-a-web-server.md | 5 ++--- src/ch21-01-single-threaded.md | 5 ++--- src/ch21-02-multithreaded.md | 5 ++--- src/ch21-03-graceful-shutdown-and-cleanup.md | 5 ++--- src/foreword.md | 5 ++--- src/title-page.md | 7 +++---- 19 files changed, 39 insertions(+), 57 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3b4a877..a418686 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,8 +25,8 @@ jobs: - name: Install mdbook & mdbook-typst-pdf run: | mkdir bin - curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.46/mdbook-v0.4.46-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin - curl -sSL https://github.com/KaiserY/mdbook-typst-pdf/releases/download/v0.6.0/mdbook-typst-pdf-x86_64-unknown-linux-musl.tar.xz | tar -xJ --directory=bin --strip-components 1 + curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.49/mdbook-v0.4.49-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin + curl -sSL https://github.com/KaiserY/mdbook-typst-pdf/releases/download/v0.6.1/mdbook-typst-pdf-x86_64-unknown-linux-musl.tar.xz | tar -xJ --directory=bin --strip-components 1 echo "$(pwd)/bin" >> ${GITHUB_PATH} - name: Install font run: | diff --git a/src/ch17-00-async-await.md b/src/ch17-00-async-await.md index 6633c80..7cad701 100644 --- a/src/ch17-00-async-await.md +++ b/src/ch17-00-async-await.md @@ -1,8 +1,7 @@ # Async 和 await -> [ch17-00-async-await.md](https://github.com/rust-lang/book/blob/main/src/ch17-00-async-await.md) ->
-> commit 3111eda07a4a4692bf69e3aaad999d840ac9c138 + + 很多我们要求计算机处理的操作都需要一定的时间才能完成。例如,如果你使用视频编辑器来创建一个家庭聚会的视频,导出视频可能会花费几分钟到几小时不等。同样,从家庭成员那里下载共享的视频也可能需要很长时间。如果我们能在等待这些长时间运行的操作完成期间做点其他事情,那就太好了。 diff --git a/src/ch17-01-futures-and-syntax.md b/src/ch17-01-futures-and-syntax.md index 509ccd6..3408754 100644 --- a/src/ch17-01-futures-and-syntax.md +++ b/src/ch17-01-futures-and-syntax.md @@ -1,8 +1,7 @@ ## Futures 和 async 语法 -> [ch17-01-futures-and-syntax.md](https://github.com/rust-lang/book/blob/main/src/ch17-01-futures-and-syntax.md) ->
-> commit e95efa05706c5c4309df9ed47d5e91d8ed342b7d + + Rust 异步编程的关键元素是 *futures* 和 Rust 的 `async` 与 `await` 关键字。 diff --git a/src/ch17-02-concurrency-with-async.md b/src/ch17-02-concurrency-with-async.md index 5b07c7c..6c8cdf5 100644 --- a/src/ch17-02-concurrency-with-async.md +++ b/src/ch17-02-concurrency-with-async.md @@ -1,8 +1,7 @@ ## 并发与 async -> [ch17-02-concurrency-with-async.md](https://github.com/rust-lang/book/blob/main/src/ch17-02-concurrency-with-async.md) ->
-> commit 62d441060d66f9a1c3d3cdfffa8eed40f817d1aa + + 在这一部分,我们将使用异步来应对一些与第十六章中通过线程解决的相同的并发问题。因为之前我们已经讨论了很多关键理念了,这一部分我们会专注于线程与 future 的区别。 diff --git a/src/ch17-03-more-futures.md b/src/ch17-03-more-futures.md index e0d9081..40d18f5 100644 --- a/src/ch17-03-more-futures.md +++ b/src/ch17-03-more-futures.md @@ -1,8 +1,7 @@ ## 使用任意数量的 futures -> [ch17-03-more-futures.md](https://github.com/rust-lang/book/blob/main/src/ch17-03-more-futures.md) ->
-> commit 9e85fcc9938e8f8c935d0ad8b4db7f45caaa2ca4 + + 当我们在上一部分从使用两个 future 到三个 future 的时候,我们也必须从使用 `join` 切换到 `join3`。每次我们想要改变 join 的 future 数量时都不得不调用一个不同的函数是很烦人的。令人高兴的是,我们有一个宏版本的 `join` 可以传递任意数量的参数。它还会自行处理 await 这些 future。因此,我们可以重写示例 17-13 中的代码来使用 `join!` 而不是 `join3`,如示例 17-14 所示: diff --git a/src/ch17-04-streams.md b/src/ch17-04-streams.md index 6a4efcc..8d0cca3 100644 --- a/src/ch17-04-streams.md +++ b/src/ch17-04-streams.md @@ -1,8 +1,7 @@ ## 流(Streams):顺序的 Futrues -> [ch17-04-streams.md](https://github.com/rust-lang/book/blob/main/src/ch17-04-streams.md) ->
-> commit 56ec353290429e6547109e88afea4de027b0f1a9 + + 到本章的目前为止,我们大部分时间都专注于单个的 future 上。一个重要的例外就是我们用过的异步信道。回忆一下在本章之前的 [“消息传递”][17-02-messages] 中我们如何使用异步信道接收端的。异步 `recv` 方法随着时间的推移产生一个序列的项。这是一个更通用的模式的实例,通常被称为 *流*(*stream*)。 diff --git a/src/ch17-05-traits-for-async.md b/src/ch17-05-traits-for-async.md index 356e8a5..d536162 100644 --- a/src/ch17-05-traits-for-async.md +++ b/src/ch17-05-traits-for-async.md @@ -1,8 +1,7 @@ ## 深入理解 async 相关的 traits -> [ch17-05-traits-for-async.md](https://github.com/rust-lang/book/blob/main/src/ch17-05-traits-for-async.md) ->
-> commit 56ec353290429e6547109e88afea4de027b0f1a9 + + 贯穿本章,我们通过多种方式使用了 `Future`、`Pin`、`Unpin`、`Stream` 和 `StreamExt` trait。但是直到目前为止,我们避免过多地了解它们如何工作或者如何组合在一起的细节,这对你日常的 Rust 开发而言通常是没问题的。不过有时你会遇到需要了解更多细节的场景。在本小节,我们会足够深入以便理解这些场景,并仍会将 *真正* 有深度的内容留给其它文档。 diff --git a/src/ch17-06-futures-tasks-threads.md b/src/ch17-06-futures-tasks-threads.md index 2e35935..903050f 100644 --- a/src/ch17-06-futures-tasks-threads.md +++ b/src/ch17-06-futures-tasks-threads.md @@ -1,8 +1,7 @@ ## 结合使用 future、任务和线程 -> [ch17-06-futures-tasks-threads.md](https://github.com/rust-lang/book/blob/main/src/ch17-06-futures-tasks-threads.md) ->
-> commit 06d73f3935dfec895aec9790127dc8b6fc827ce1 + + 正如我们在[第十六章][ch16]所见,线程提供了一种并发的方式。在这一章节我们见过了另一种方式:通过 future 和流来使用异步。如果你好奇何时选择一个而不是另一个,答案是:视具体情况而定!同时在很多场景下,选择并非线程**或**异步而是线程**和**异步。 diff --git a/src/ch20-01-unsafe-rust.md b/src/ch20-01-unsafe-rust.md index 0ebab4e..8b5c493 100644 --- a/src/ch20-01-unsafe-rust.md +++ b/src/ch20-01-unsafe-rust.md @@ -1,8 +1,7 @@ ## 不安全 Rust -> [ch20-01-unsafe-rust.md](https://github.com/rust-lang/book/blob/main/src/ch20-01-unsafe-rust.md) ->
-> commit 4b2590f90c1d72002278a8a426a94a9eadd4d498 + + 目前为止讨论过的代码都有 Rust 在编译时会强制执行的内存安全保证。然而,Rust 还隐藏有第二种语言,它不会强制执行这类内存安全保证:这被称为 **不安全 Rust**(*unsafe Rust*)。它与常规 Rust 代码无异,但是会提供额外的超能力。 diff --git a/src/ch20-02-advanced-traits.md b/src/ch20-02-advanced-traits.md index 7a7a332..6c0955f 100644 --- a/src/ch20-02-advanced-traits.md +++ b/src/ch20-02-advanced-traits.md @@ -1,8 +1,7 @@ ## 高级 trait -> [ch20-02-advanced-traits.md](https://github.com/rust-lang/book/blob/main/src/ch20-02-advanced-traits.md) ->
-> commit 56ec353290429e6547109e88afea4de027b0f1a9 + + 在第十章[“trait:定义共同行为”][traits-defining-shared-behavior]部分,我们第一次涉及到了 trait,不过我们并没有覆盖一些较为高级的细节。现在你对 Rust 已经有了更多了解,我们可以深入探究了。 diff --git a/src/ch20-03-advanced-types.md b/src/ch20-03-advanced-types.md index 03566d6..5822106 100644 --- a/src/ch20-03-advanced-types.md +++ b/src/ch20-03-advanced-types.md @@ -1,8 +1,7 @@ ## 高级类型 -> [ch20-03-advanced-types.md](https://github.com/rust-lang/book/blob/main/src/ch20-03-advanced-types.md) ->
-> commit 56ec353290429e6547109e88afea4de027b0f1a9 + + Rust 的类型系统有一些我们曾经提到但尚未讨论过的特性。首先我们将从一般意义上讨论 newtype 并探讨它们作为类型为何有用。接着会转向类型别名(type aliases),一个类似于 newtype 但有着稍微不同的语义的功能。我们还会讨论 `!` 类型和动态大小类型。 diff --git a/src/ch20-04-advanced-functions-and-closures.md b/src/ch20-04-advanced-functions-and-closures.md index 4c6ff8f..51c415d 100644 --- a/src/ch20-04-advanced-functions-and-closures.md +++ b/src/ch20-04-advanced-functions-and-closures.md @@ -1,8 +1,7 @@ ## 高级函数与闭包 -> [ch20-04-advanced-functions-and-closures.md](https://github.com/rust-lang/book/blob/main/src/ch20-04-advanced-functions-and-closures.md) ->
-> commit 56ec353290429e6547109e88afea4de027b0f1a9 + + 本部分将探索一些有关函数和闭包的高级特性,这包括函数指针以及返回闭包。 diff --git a/src/ch20-05-macros.md b/src/ch20-05-macros.md index 8e68d35..bda06f5 100644 --- a/src/ch20-05-macros.md +++ b/src/ch20-05-macros.md @@ -1,8 +1,7 @@ ## 宏 -> [ch20-05-macros.md](https://github.com/rust-lang/book/blob/main/src/ch20-05-macros.md) ->
-> commit 1d1424ba1c30b8efab636c911be0a215df305eea + + 我们已经在本书中使用过像 `println!` 这样的宏了,不过尚未深入探讨什么是宏以及它是如何工作的。**宏**(*Macro*)指的是 Rust 中一系列的功能:使用 `macro_rules!` 的 **声明宏**(*declarative macro*),和三种 **过程宏**(*procedural macro*): diff --git a/src/ch21-00-final-project-a-web-server.md b/src/ch21-00-final-project-a-web-server.md index 3bd491b..dbfbb23 100644 --- a/src/ch21-00-final-project-a-web-server.md +++ b/src/ch21-00-final-project-a-web-server.md @@ -1,8 +1,7 @@ # 最后的项目:构建多线程 web server -> [ch21-00-final-project-a-web-server.md](https://github.com/rust-lang/book/blob/main/src/ch21-00-final-project-a-web-server.md) ->
-> commit 56ec353290429e6547109e88afea4de027b0f1a9 + + 这是一次漫长的旅途,不过我们已经抵达了本书的结尾。在本章中,我们将一同构建另一个项目,来展示最后几章所学,同时复习更早的章节。 diff --git a/src/ch21-01-single-threaded.md b/src/ch21-01-single-threaded.md index fd7734b..4d13fa5 100644 --- a/src/ch21-01-single-threaded.md +++ b/src/ch21-01-single-threaded.md @@ -1,8 +1,7 @@ ## 构建单线程 web server -> [ch21-01-single-threaded.md](https://github.com/rust-lang/book/blob/main/src/ch21-01-single-threaded.md) ->
-> commit bf3d2256c82408e89f5cfdaf8ec212739c255dd3 + + 首先让我们创建一个可运行的单线程 web server。在开始之前,我们将快速了解一下构建 web server 所涉及到的协议。这些协议的细节超出了本书的范畴,不过一个简单的概括会提供我们所需的信息。 diff --git a/src/ch21-02-multithreaded.md b/src/ch21-02-multithreaded.md index 095879b..96c35ff 100644 --- a/src/ch21-02-multithreaded.md +++ b/src/ch21-02-multithreaded.md @@ -1,8 +1,7 @@ ## 将单线程 server 变为多线程 server -> [ch21-02-multithreaded.md](https://github.com/rust-lang/book/blob/main/src/ch21-02-multithreaded.md) ->
-> commit 56ec353290429e6547109e88afea4de027b0f1a9 + + 目前服务端会依次处理每一个请求,意味着它在完成第一个连接的处理之前不会处理第二个连接。如果服务端正接收越来越多的请求,这类串行操作会使性能越来越差。如果一个请求花费很长时间来处理,随后而来的请求则不得不等待这个长请求结束,即便这些新请求可以很快就处理完。我们需要修复这种情况,不过首先让我们实际尝试一下这个问题。 diff --git a/src/ch21-03-graceful-shutdown-and-cleanup.md b/src/ch21-03-graceful-shutdown-and-cleanup.md index 9aab6ec..aacc3ca 100644 --- a/src/ch21-03-graceful-shutdown-and-cleanup.md +++ b/src/ch21-03-graceful-shutdown-and-cleanup.md @@ -1,8 +1,7 @@ ## 优雅停机与清理 -> [ch21-03-graceful-shutdown-and-cleanup.md](https://github.com/rust-lang/book/blob/main/src/ch21-03-graceful-shutdown-and-cleanup.md) ->
-> commit 5d22a358fb2380aa3f270d7b6269b67b8e44849e + + 示例 21-20 中的代码如期通过使用线程池异步的响应请求。这里有一些警告说 `workers`、`id` 和 `thread` 字段没有直接被使用,这提醒了我们并没有清理所有的内容。当使用不那么优雅的 ctrl-c 终止主线程时,所有其他线程也会立刻停止,即便它们正处于处理请求的过程中。 diff --git a/src/foreword.md b/src/foreword.md index 7f30410..86059ce 100644 --- a/src/foreword.md +++ b/src/foreword.md @@ -1,8 +1,7 @@ # 前言 -> [foreword.md](https://github.com/rust-lang/book/blob/main/src/foreword.md) ->
-> commit 1fedfc4b96c2017f64ecfcf41a0a07e2e815f24f + + Rust 程序设计语言的本质实际在于 **赋能**(*empowerment*):无论你现在编写的是何种代码,Rust 能让你在更为广泛的编程领域走得更远,写出自信。(这一点并不显而易见) diff --git a/src/title-page.md b/src/title-page.md index 4c7080c..1862d48 100644 --- a/src/title-page.md +++ b/src/title-page.md @@ -1,8 +1,7 @@ # Rust 程序设计语言 -> [title-page.md](https://github.com/rust-lang/book/blob/main/src/title-page.md) ->
-> commit d94e03a18a2590ed3f1c67b859cb11528d2a2d5c + + **本书的英文原版作者为 Steve Klabnik 和 Carol Nichols,并由 Rust 社区补充完善。本简体中文译本由 Rust 中文社区翻译。** @@ -10,7 +9,7 @@ 本书的英文原版 HTML 格式可以在 [https://doc.rust-lang.org/stable/book/](https://doc.rust-lang.org/stable/book/) 在线阅读;使用 `rustup` 安装的 Rust 也包含一份英文离线版,运行 `rustup docs --book` 即可打开。 -本书还有一些社区 [翻译版本][translations]。简体中文译本可以在 [https://kaisery.github.io/trpl-zh-cn/](https://kaisery.github.io/trpl-zh-cn/) 在线阅读。 +本书还有一些社区 [翻译版本][translations]。(译者注:简体中文译本可以在 [https://kaisery.github.io/trpl-zh-cn/](https://kaisery.github.io/trpl-zh-cn/) 在线阅读,PDF 版本请下载 [Rust 程序设计语言 简体中文版.pdf](https://kaisery.github.io/trpl-zh-cn/Rust%20%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E8%AF%AD%E8%A8%80%20%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%E7%89%88.pdf)) 本书也有[由 No Starch Press 出版的纸质版和电子版][nsprust]。