5 月 15 日是 Rust 1.0 发布十周年纪念日,Rust 项目开发者在荷兰的 Utrecht 举办了“Rust 十周年”庆祝活动,并在当天发布新版本 1.87.0。
新版本的主要新特性包括:
- 标准库加入匿名管道(Anonymous Pipes)
use std::io::Read;
let (mut recv, send) = std::io::pipe()?;
let mut command = Command::new("path/to/bin")
// Both stdout and stderr will write to the same pipe, combining the two.
.stdout(send.try_clone()?)
.stderr(send)
.spawn()?;
let mut output = Vec::new();
recv.read_to_end(&mut output)?;
// It's important that we read from the pipe before the process exits, to avoid
// filling the OS buffers if the program emits too much output.
assert!(command.wait()?.success());
- 安全架构 intrinsics
#![forbid(unsafe_op_in_unsafe_fn)]
use std::arch::x86_64::*;
fn sum(slice: &[u32]) -> u32 {
#[cfg(target_arch = "x86_64")]
{
if is_x86_feature_detected!("avx2") {
// SAFETY: We have detected the feature is enabled at runtime,
// so it's safe to call this function.
return unsafe { sum_avx2(slice) };
}
}
slice.iter().sum()
}
#[target_feature(enable = "avx2")]
#[cfg(target_arch = "x86_64")]
fn sum_avx2(slice: &[u32]) -> u32 {
// SAFETY: __m256i and u32 have the same validity.
let (prefix, middle, tail) = unsafe { slice.align_to::<__m256i>() };
let mut sum = prefix.iter().sum::<u32>();
sum += tail.iter().sum::<u32>();
// Core loop is now fully safe code in 1.87, because the intrinsics require
// matching target features (avx2) to the function definition.
let mut base = _mm256_setzero_si256();
for e in middle.iter() {
base = _mm256_add_epi32(base, *e);
}
// SAFETY: __m256i and u32 have the same validity.
let base: [u32; 8] = unsafe { std::mem::transmute(base) };
sum += base.iter().sum::<u32>();
sum
}
- 通过
asm!
内联汇编可跳转到 Rust 代码中的标记块
unsafe {
asm!(
"jmp {}",
label {
println!("Jumped from asm!");
}
);
}
- 稳定 API 等等
为庆祝 Rust 1.0 稳定版发布十周年,Rust 作者 Graydon Hoare 写了一篇《10 Years of Stable Rust: An Infrastructure Story》长文进行回顾,他在文章提到了一组数据:
-
1.0 之前,Rust 代码库累计了 4 万次提交;此后又新增了 24.6 万次提交。换算下来,过去 10 年几乎是每小时合并 2.8 次提交。
-
贡献者从 1.0 时不足 1000 人,扩展到现在约 6700 人。
-
项目已关闭超过 4.7 万个 issue,合并了 14 万多个 PR。
-
1.0 时共计约 1100 份 RFC(用于语言演进的提案),如今累计达到了 3772 份。
-
发布了 87 个正式版本,大多数都按六周节奏准时发布。
-
推出过 3 个 Edition(版本变更打包,兼容旧代码),用于引入需要 opt-in 的非兼容变更。
-
每个版本的兼容性测试范围从 2500 个 crate 增长到了现在的 58.7 万个。