3.5.3 有时很有用

03-有时很有用 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/unsafe-deep-dive/introduction/characteristics-of-unsafe-rust/sometimes-useful.html

3.5.3 有时很有用

你的代码可以跑得更快!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright 2026 Google LLC
// SPDX-License-Identifier: Apache-2.0
fn iter_sum(xs: &[u64]) -> u64 {
    xs.iter().sum()
}

fn fast_sum(xs: &[u64]) -> u64 {
    let mut acc = 0;
    let mut i = 0;
    unsafe {
        while i < xs.len() {
            acc += *xs.get_unchecked(i);
            i += 1;
        }
    }
    acc
}

fn main() {
    let data: Vec<_> = (0..1_000_000).collect();

    let baseline = iter_sum(&data);
    let unchecked = fast_sum(&data);

    assert_eq!(baseline, unchecked);
}

使用 unsafe 的代码 可能 更快。

fast_sum() 跳过了 bounds check(边界检查)。然而,要验证性能主张,必须进行 benchmark(基准测试)。对于这类情况,Rust 的 iterator(迭代器)通常也能消除边界检查。

可选:展示两个函数生成的相同汇编。

最后修改 August 11, 2026: 更新 (70a5af133)