4.4.1 示例:ASCII 类型
01-示例:ASCII 类型 — Comprehensive Rust
1分钟内可阅读完
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/unsafe-deep-dive/safety-preconditions/ascii.html
4.4.1 示例:ASCII 类型
| |
「
Ascii类型是对字节切片的极简包装。内部表示相同。但Ascii要求最高位不能置位。」可选:扩展示例,说明可以在测试中使用
debug_assert!检查前置条件,而不影响 release 构建。
1 2 3 4 5 6// Copyright 2026 Google LLC // SPDX-License-Identifier: Apache-2.0 unsafe fn new_unchecked(bytes: &mut [u8]) -> Self { debug_assert!(bytes.iter().all(|&b| b.is_ascii())) Ascii(bytes) }