|
| 1 | +# Rust 新版解读 | 1.87 | 十周年🎉 |
| 2 | + |
| 3 | +[Rust 1.0](https://blog.rust-lang.org/2015/05/15/Rust-1.0/) 十周年🎉 |
| 4 | + |
| 5 | +> Rust 1.87 官方 release doc: [Announcing Rust 1.87.0 | Rust Blog](https://blog.rust-lang.org/2025/05/15/Rust-1.87.0/) |
| 6 | +
|
| 7 | +通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.87 版本: |
| 8 | + |
| 9 | +```shell |
| 10 | +$ rustup update stable |
| 11 | +``` |
| 12 | + |
| 13 | +## 匿名管道 |
| 14 | + |
| 15 | +1.87 版本为标准库添加了匿名管道支持,包括与 `std::process::Command` 输入/输出方法的集成。例如,现在可以相对简单地合并标准输出和标准错误流,如下所示,而过去需要额外线程或平台特定函数才能实现。 |
| 16 | + |
| 17 | +```rust |
| 18 | +use std::process::Command; |
| 19 | +use std::io::Read; |
| 20 | + |
| 21 | +let (mut recv, send) = std::io::pipe()?; |
| 22 | + |
| 23 | +let mut command = Command::new("path/to/bin") |
| 24 | + // 标准输出和标准错误都会写入同一个管道,实现合并 |
| 25 | + .stdout(send.try_clone()?) |
| 26 | + .stderr(send) |
| 27 | + .spawn()?; |
| 28 | + |
| 29 | +let mut output = Vec::new(); |
| 30 | +recv.read_to_end(&mut output)?; |
| 31 | + |
| 32 | +// 必须在进程退出前读取管道内容,避免程序输出过多时填满系统缓冲区 |
| 33 | +assert!(command.wait()?.success()); |
| 34 | +``` |
| 35 | + |
| 36 | +## 安全的架构内置函数 |
| 37 | + |
| 38 | +大多数仅因需要启用目标特性而被标记为不安全的 `std::arch` 内置函数,现在可以在已启用相应特性的安全代码中调用。例如,以下使用手动内置函数实现数组求和的示例程序,现在核心循环可以使用安全代码。 |
| 39 | + |
| 40 | +```rust |
| 41 | +#![forbid(unsafe_op_in_unsafe_fn)] |
| 42 | + |
| 43 | +use std::arch::x86_64::*; |
| 44 | + |
| 45 | +fn sum(slice: &[u32]) -> u32 { |
| 46 | + #[cfg(target_arch = "x86_64")] |
| 47 | + { |
| 48 | + if is_x86_feature_detected!("avx2") { |
| 49 | + // 安全性:我们已检测到运行时启用了该特性,因此调用此函数是安全的 |
| 50 | + return unsafe { sum_avx2(slice) }; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + slice.iter().sum() |
| 55 | +} |
| 56 | + |
| 57 | +#[target_feature(enable = "avx2")] |
| 58 | +#[cfg(target_arch = "x86_64")] |
| 59 | +fn sum_avx2(slice: &[u32]) -> u32 { |
| 60 | + // 安全性:__m256i 和 u32 具有相同的有效性 |
| 61 | + let (prefix, middle, tail) = unsafe { slice.align_to::<__m256i>() }; |
| 62 | + |
| 63 | + let mut sum = prefix.iter().sum::<u32>(); |
| 64 | + sum += tail.iter().sum::<u32>(); |
| 65 | + |
| 66 | + // 在 1.87 中核心循环现在是完全安全的代码,因为内置函数要求与函数定义匹配的目标特性 (avx2) |
| 67 | + let mut base = _mm256_setzero_si256(); |
| 68 | + for e in middle.iter() { |
| 69 | + base = _mm256_add_epi32(base, *e); |
| 70 | + } |
| 71 | + |
| 72 | + // 安全性:__m256i 和 u32 具有相同的有效性 |
| 73 | + let base: [u32; 8] = unsafe { std::mem::transmute(base) }; |
| 74 | + sum += base.iter().sum::<u32>(); |
| 75 | + |
| 76 | + sum |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## `asm!` 跳转到 Rust 代码 |
| 81 | + |
| 82 | +内联汇编 (`asm!`) 现在可以跳转到 Rust 代码中的 labeled 代码块。这为底层编程提供了更大灵活性,例如在操作系统内核中实现优化控制流,或更高效地与硬件交互。 |
| 83 | + |
| 84 | +- `asm!` 宏现在支持 label 标签语法,作为跳转目标 |
| 85 | +- label 必须是返回类型为 `()` 或 `!` 的块表达式 |
| 86 | +- 跳转时会执行该块,然后继续执行 `asm!` 块之后的代码 |
| 87 | +- 在同一 `asm!` 里使用调用中使用 output 和 label 仍处于[unstable](https://github.com/rust-lang/rust/issues/119364)的 |
| 88 | + |
| 89 | +```rust |
| 90 | +unsafe { |
| 91 | + asm!( |
| 92 | + "jmp {}", |
| 93 | + label { |
| 94 | + println!("从汇编跳转而来!"); |
| 95 | + } |
| 96 | + ); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +更多细节请参阅[参考文档](https://doc.rust-lang.org/nightly/reference/inline-assembly.html#r-asm.operand-type.supported-operands.label)。 |
| 101 | + |
| 102 | +## 特征定义中 `impl Trait` 的精确捕获 (`+ use<...>`) |
| 103 | + |
| 104 | +本版本稳定了在特征定义中使用 `impl Trait` 返回类型时指定具体捕获的泛型类型和生命周期的功能。这扩展了 [1.82](https://blog.rust-lang.org/2024/10/17/Rust-1.82.0/#precise-capturing-use-syntax) 版本中对非特征函数的稳定支持。 |
| 105 | + |
| 106 | +一些示例解语法: |
| 107 | + |
| 108 | +```rust |
| 109 | +trait Foo { |
| 110 | + fn method<'a>(&'a self) -> impl Sized; |
| 111 | + |
| 112 | + // ... 解语法后类似: |
| 113 | + type Implicit1<'a>: Sized; |
| 114 | + fn method_desugared<'a>(&'a self) -> Self::Implicit1<'a>; |
| 115 | + |
| 116 | + // ... 而使用精确捕获时 ... |
| 117 | + fn precise<'a>(&'a self) -> impl Sized + use<Self>; |
| 118 | + |
| 119 | + // ... 解语法后类似: |
| 120 | + type Implicit2: Sized; |
| 121 | + fn precise_desugared<'a>(&'a self) -> Self::Implicit2; |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Others |
| 126 | + |
| 127 | +其它更新细节,和稳定的 API 列表,参考[原Blog](https://blog.rust-lang.org/2025/05/15/Rust-1.87.0/#stabilized-apis) |
0 commit comments