知识卡片
文本有三种处理单位
UTF-8 文本可按字节存储、按 Unicode 标量遍历,也可按用户感知的字素簇分段。Rust 的 char 表示标量,一个字素簇可能由多个 char 组成。len() 数字节,chars() 遍历标量,bytes() 遍历字节。整数下标会混淆这些单位,并且第 n 个标量通常需从头扫描,不能保证常数时间。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 常用集合 Indexing into Strings · 查看原书依据
Another point about UTF-8 is that there are actually three relevant ways to look at strings from Rust’s perspective: as bytes, scalar values, and grapheme clusters (the closest thing to what we would call letters).
参考来源
- 位置:《The Rust Programming Language(第2版)》第08章 常用集合(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c08.xhtml`,块 0135)
- 结论依据:原文分别定义字节、标量值、字素簇三种视角,并结合天城文展示数量差异。
- 原始内容:Another point about UTF-8 is that there are actually three relevant ways to look at strings from Rust’s perspective: as bytes, scalar values, and grapheme clusters (the closest thing to what we would call letters).