From a0aa47fdee4adb0f777b170fca5fd91ca733f5ab Mon Sep 17 00:00:00 2001 From: Allan Downey Date: Sun, 29 May 2022 23:28:27 +0800 Subject: [PATCH 1/2] add 1.61 release note --- src/SUMMARY.md | 117 ++++++++++++++++------------- src/appendix/rust-versions/1.61.md | 69 +++++++++++++++++ 2 files changed, 134 insertions(+), 52 deletions(-) create mode 100644 src/appendix/rust-versions/1.61.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index c7579787..753d6402 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -5,13 +5,16 @@ [快速查询入口](index-list.md) --- + [Rust 语言中文网](community.md) [一本生锈的书](rusty-book.md) [Rust 语言周刊](rust-weekly.md) [Rust 翻译计划( 代号 Rustt )](rustt.md) # 快速开始 + --- + - [寻找牛刀,以便小试](first-try/intro.md) - [安装 Rust 环境](first-try/installation.md) - [墙推 VSCode!](first-try/editor.md) @@ -19,10 +22,13 @@ - [不仅仅是 Hello world](first-try/hello-world.md) - [下载依赖太慢了?](first-try/slowly-downloading.md) - [避免从入门到放弃](first-try/sth-you-should-not-do.md) - -# Rust语言学习 + +# Rust 语言学习 + --- + - [Rust 基础入门](basic/intro.md) + - [变量绑定与解构](basic/variable.md) - [基本类型](basic/base-type/index.md) - [数值类型](basic/base-type/numbers.md) @@ -65,6 +71,7 @@ - [格式化输出](basic/formatted-output.md) - [Rust 高级进阶](advance/intro.md) + - [生命周期](advance/lifetime/intro.md) - [认识生命周期](advance/lifetime/basic.md) - [深入生命周期](advance/lifetime/advance.md) @@ -102,8 +109,9 @@ - [Macro 宏编程](advance/macro.md) - + - [Rust 异步编程](async-rust/intro.md) + - [async/await 异步编程](async-rust/async/intro.md) - [async 编程入门](async-rust/async/getting-started.md) - [底层探秘: Future 执行与任务调度](async-rust/async/future-excuting.md) @@ -126,7 +134,6 @@ - [优雅的关闭](async-rust/tokio/graceful-shutdown.md) - [异步跟同步共存](async-rust/tokio/bridging-with-sync.md) - - [Rust 难点攻关](difficulties/intro.md) - [切片和切片引用](difficulties/slice.md) - [Eq 和 PartialEq](difficulties/eq.md) @@ -135,11 +142,12 @@ - [move、Copy 和 Clone TODO](difficulties/move-copy.md) - [裸指针、引用和智能指针 TODO](advance/difficulties/pointer.md) - - # 常用工具链 + --- + - [自动化测试](test/intro.md) + - [编写测试及控制执行](test/write-tests.md) - [单元测试和集成测试](test/unit-integration-test.md) - [断言 assertion](test/assertion.md) @@ -169,66 +177,69 @@ - [通过 config.toml 对 Cargo 进行配置](cargo/reference/configuration.md) - [发布到 crates.io](cargo/reference/publishing-on-crates.io.md) - [构建脚本 build.rs](cargo/reference/build-script/intro.md) - - [构建脚本示例](cargo/reference/build-script/examples.md) + - [构建脚本示例](cargo/reference/build-script/examples.md) # 开发实践 + --- + - [企业落地实践](usecases/intro.md) + - [AWS 为何这么喜欢 Rust?](usecases/aws-rust.md) - [日志和监控](logs/intro.md) - - [日志详解](logs/about-log.md) - - [日志门面 log](logs/log.md) - - [使用 tracing 记录日志](logs/tracing.md) - - [自定义 tracing 的输出格式](logs/tracing-logger.md) - - [监控](logs/observe/intro.md) - - [可观测性](logs/observe/about-observe.md) - - [分布式追踪](logs/observe/trace.md) + - [日志详解](logs/about-log.md) + - [日志门面 log](logs/log.md) + - [使用 tracing 记录日志](logs/tracing.md) + - [自定义 tracing 的输出格式](logs/tracing-logger.md) + - [监控](logs/observe/intro.md) + - [可观测性](logs/observe/about-observe.md) + - [分布式追踪](logs/observe/trace.md) - [Rust 最佳实践](practice/intro.md) - [日常开发三方库精选](practice/third-party-libs.md) - [命名规范](practice/naming.md) - [面试经验](practice/interview.md) - [代码开发实践 todo](practice/best-pratice.md) - - [手把手带你实现链表](too-many-lists/intro.md) - - [我们到底需不需要链表](too-many-lists/do-we-need-it.md) - - [不太优秀的单向链表:栈](too-many-lists/bad-stack/intro.md) - - [数据布局](too-many-lists/bad-stack/layout.md) - - [基本操作](too-many-lists/bad-stack/basic-operations.md) - - [最后实现](too-many-lists/bad-stack/final-code.md) - - [还可以的单向链表](too-many-lists/ok-stack/intro.md) - - [优化类型定义](too-many-lists/ok-stack/type-optimizing.md) - - [定义 Peek 函数](too-many-lists/ok-stack/peek.md) - - [IntoIter 和 Iter](too-many-lists/ok-stack/iter.md) - - [IterMut以及完整代码](too-many-lists/ok-stack/itermut.md) - - [持久化单向链表](too-many-lists/persistent-stack/intro.md) - - [数据布局和基本操作](too-many-lists/persistent-stack/layout.md) - - [Drop、Arc 及完整代码](too-many-lists/persistent-stack/drop-arc.md) - - [不咋样的双端队列](too-many-lists/deque/intro.md) - - [数据布局和基本操作](too-many-lists/deque/layout.md) - - [Peek](too-many-lists/deque/peek.md) - - [基本操作的对称镜像](too-many-lists/deque/symmetric.md) - - [迭代器](too-many-lists/deque/iterator.md) - - [最终代码](too-many-lists/deque/final-code.md) - - [不错的unsafe队列](too-many-lists/unsafe-queue/intro.md) - - [数据布局](too-many-lists/unsafe-queue/layout.md) - - [基本操作](too-many-lists/unsafe-queue/basics.md) - - [Miri](too-many-lists/unsafe-queue/miri.md) - - [栈借用](too-many-lists/unsafe-queue/stacked-borrow.md) - - [测试栈借用](too-many-lists/unsafe-queue/testing-stacked-borrow.md) - - [数据布局2](too-many-lists/unsafe-queue/layout2.md) - - [额外的操作](too-many-lists/unsafe-queue/extra-junk.md) - - [最终代码](too-many-lists/unsafe-queue/final-code.md) - - [使用高级技巧实现链表](too-many-lists/advanced-lists/intro.md) - - [生产级可用的双向链表](too-many-lists/advanced-lists/unsafe-deque.md) - - [双单向链表](too-many-lists/advanced-lists/double-singly.md) - - [栈上的链表](too-many-lists/advanced-lists/stack-allocated.md) - + - [我们到底需不需要链表](too-many-lists/do-we-need-it.md) + - [不太优秀的单向链表:栈](too-many-lists/bad-stack/intro.md) + - [数据布局](too-many-lists/bad-stack/layout.md) + - [基本操作](too-many-lists/bad-stack/basic-operations.md) + - [最后实现](too-many-lists/bad-stack/final-code.md) + - [还可以的单向链表](too-many-lists/ok-stack/intro.md) + - [优化类型定义](too-many-lists/ok-stack/type-optimizing.md) + - [定义 Peek 函数](too-many-lists/ok-stack/peek.md) + - [IntoIter 和 Iter](too-many-lists/ok-stack/iter.md) + - [IterMut 以及完整代码](too-many-lists/ok-stack/itermut.md) + - [持久化单向链表](too-many-lists/persistent-stack/intro.md) + - [数据布局和基本操作](too-many-lists/persistent-stack/layout.md) + - [Drop、Arc 及完整代码](too-many-lists/persistent-stack/drop-arc.md) + - [不咋样的双端队列](too-many-lists/deque/intro.md) + - [数据布局和基本操作](too-many-lists/deque/layout.md) + - [Peek](too-many-lists/deque/peek.md) + - [基本操作的对称镜像](too-many-lists/deque/symmetric.md) + - [迭代器](too-many-lists/deque/iterator.md) + - [最终代码](too-many-lists/deque/final-code.md) + - [不错的 unsafe 队列](too-many-lists/unsafe-queue/intro.md) + - [数据布局](too-many-lists/unsafe-queue/layout.md) + - [基本操作](too-many-lists/unsafe-queue/basics.md) + - [Miri](too-many-lists/unsafe-queue/miri.md) + - [栈借用](too-many-lists/unsafe-queue/stacked-borrow.md) + - [测试栈借用](too-many-lists/unsafe-queue/testing-stacked-borrow.md) + - [数据布局 2](too-many-lists/unsafe-queue/layout2.md) + - [额外的操作](too-many-lists/unsafe-queue/extra-junk.md) + - [最终代码](too-many-lists/unsafe-queue/final-code.md) + - [使用高级技巧实现链表](too-many-lists/advanced-lists/intro.md) + - [生产级可用的双向链表](too-many-lists/advanced-lists/unsafe-deque.md) + - [双单向链表](too-many-lists/advanced-lists/double-singly.md) + - [栈上的链表](too-many-lists/advanced-lists/stack-allocated.md) # 高级专题 + --- - [征服编译错误](compiler/intro.md) + - [对抗编译检查](compiler/fight-with-compiler/intro.md) - [生命周期](compiler/fight-with-compiler/lifetime/intro.md) - [生命周期过大-01](compiler/fight-with-compiler/lifetime/too-long1.md) @@ -254,6 +265,7 @@ - [警惕 UTF-8 引发的性能隐患](compiler/pitfalls/utf8-performance.md) - [Rust 性能优化 todo](profiling/intro.md) + - [深入内存 todo](profiling/memory/intro.md) - [指针和引用 todo](profiling/memory/pointer-ref.md) - [未初始化内存 todo](profiling/memory/uninit.md) @@ -280,13 +292,12 @@ - [Option 枚举 todo](profiling/compiler/optimization/option.md) - [标准库解析 todo](std/intro.md) + - [标准库使用最佳时间 todo](std/search.md) - [Vector 常用方法 todo](std/vector.md) - [HashMap todo](std/hashmap.md) - [Iterator 常用方法 todo](std/iterator.md) - - - # 附录 + --- - [Appendix]() @@ -328,4 +339,6 @@ - [Rust 历次版本更新解读](appendix/rust-versions/intro.md) - [1.58](appendix/rust-versions/1.58.md) - [1.59](appendix/rust-versions/1.59.md) - - [1.60](appendix/rust-versions/1.60.md) \ No newline at end of file + - [1.60](appendix/rust-versions/1.60.md) + - [1.61](appendix/rust-versions/1.61.md) + diff --git a/src/appendix/rust-versions/1.61.md b/src/appendix/rust-versions/1.61.md new file mode 100644 index 00000000..464299f1 --- /dev/null +++ b/src/appendix/rust-versions/1.61.md @@ -0,0 +1,69 @@ +# Rust 新版解读 | 1.61 | 重点: 自定义 main 函数 ExitCode、const fn 增强、为锁定的 stdio 提供静态句柄 + +> 原文链接: https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html + +通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.61 版本: + +```shell +$ rustup update stable +``` + +## 支持自定义 main 函数 ExitCode + +一开始, Rust `main` 函数只能返回单元类型 `()`(隐式或显式),总是指示成功的退出状态,如果您要你想要其它的,必须调用 `process::exit(code)`。从 Rust 1.26 开始, `main` 允许返回一个 `Result` ,其中 `Ok` 转换为 `C EXIT_SUCCESS`,`Err` 转换为 `EXIT_FAILURE`(也调试打印错误)。在底层,这些返回类型统一使用不稳定的 `Termination` 特征。 + +在此版本中,最终稳定了 `Termination` 特征,以及一个更通用的 `ExitCode` 类型,它封装了特定于平台的返回类型。它具有 `SUCCESS` 和 `FAILURE` 常量,并为更多任意值实现 `From`。也可以为您自己的类型实现 `Termination` 特征,允许您在转换为 `ExitCode` 之前定制任何类型的报告。 + +例如,下面是一种类型安全的方式来编写 `git bisect` 运行脚本的退出代码: + +```rust +use std::process::{ExitCode, Termination}; + +#[repr(u8)] +pub enum GitBisectResult { + Good = 0, + Bad = 1, + Skip = 125, + Abort = 255, +} + +impl Termination for GitBisectResult { + fn report(self) -> ExitCode { + // Maybe print a message here + ExitCode::from(self as u8) + } +} + +fn main() -> GitBisectResult { + std::panic::catch_unwind(|| { + todo!("test the commit") + }).unwrap_or(GitBisectResult::Abort) +} +``` + +## const fn 增强 + +这个版本稳定了几个增量特性,以支持 const 函数的更多功能: + +- `fn` 指针的基本处理:现在可以在 `const fn` 中创建、传递和强制转换函数指针。例如,在为解释器构建编译时函数表时,这可能很有用。但是,仍然不允许调用 `fn` 指针。 +- 特征约束:现在可以将特征约束写在 `const fn` 的泛型参数上,如 `T: Copy`,以前只允许 `Sized`。 +- `dyn Trait` 类型:类似地,`const fn` 现在可以处理特征对象 `dyn Trait`。 +- `impl Trait` 类型:`const fn` 的参数和返回值现在可以是不透明的 `impl Trait` 类型。 + +注意,特征特性还不支持在 `const fn` 中调用这些特征的方法。 + +## 为锁定的 stdio 提供静态句柄 + +三种标准 I/O 流 —— `Stdin` 、`Stdout` 和 `Stderr` —— 都有一个 `锁(&self)`,允许对同步读写进行更多控制。但是,它们返回的锁守卫具有从 `&self` 借来的生命周期,因此它们被限制在原始句柄的范围内。这被认为是一个不必要的限制,因为底层锁实际上是在静态存储中,所以现在守卫返回一个 `'static` 生命期,与句柄断开连接。 + +例如,一个常见的错误来自于试图获取一个句柄并将其锁定在一个语句中: + +```rust +// error[E0716]: temporary value dropped while borrowed +let out = std::io::stdout().lock(); +// ^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement +// | +// creates a temporary which is freed while still in use +``` + +现在锁守卫是 `'static`,而不是借用那个临时的,所以这个可以正常工作! From 52bd2d1692c642131c05bd10b976bb25c1413059 Mon Sep 17 00:00:00 2001 From: Sunface Date: Mon, 30 May 2022 09:14:03 +0800 Subject: [PATCH 2/2] Update src/appendix/rust-versions/1.61.md --- src/appendix/rust-versions/1.61.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/appendix/rust-versions/1.61.md b/src/appendix/rust-versions/1.61.md index 464299f1..ce158c55 100644 --- a/src/appendix/rust-versions/1.61.md +++ b/src/appendix/rust-versions/1.61.md @@ -1,6 +1,7 @@ # Rust 新版解读 | 1.61 | 重点: 自定义 main 函数 ExitCode、const fn 增强、为锁定的 stdio 提供静态句柄 > 原文链接: https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html +> 翻译 by :[AllanDowney](https://github.com/AllanDowney) 通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.61 版本: