5.2.5 狼来了

05-狼来了 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/unsafe-deep-dive/rules-of-the-game/copying-memory/crying-wolf.html

5.2.5 狼来了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Copyright 2026 Google LLC
// SPDX-License-Identifier: Apache-2.0
pub unsafe fn copy(dest: &mut [u8], source: &[u8]) {
    for (dest, src) in dest.iter_mut().zip(source) {
        *dest = *src;
    }
}

fn main() {
    let a = &[114, 117, 115, 116];
    let b = &mut [82, 85, 83, 84];

    println!("{}", String::from_utf8_lossy(b));
    unsafe { copy(b, a) };
    println!("{}", String::from_utf8_lossy(b));
}

「也可以创建所谓的『狼来了』函数。

「这类函数被标记为 unsafe,但程序员无需检查任何安全前置条件。

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