知识卡片

普通读书笔记卡 · 10883.b.1 · 生成模型:未记录

字符串类型维护编码约束

理解等级:中级

String 持有可增长的 UTF-8 字节缓冲区。&str 借用一段合法 UTF-8,记录地址与字节数,字符串字面量提供的也是这种引用。String、&str 与任意字节容器的区别在于编码保证。共享字符串引用适合读取和传参,需要在运行时生成或增长文本时使用拥有缓冲区的 String。

关联阅读:[[文本字节数与标量数]]、[[文本类型匹配外部边界]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第02章《Fundamental Types》|字符的标量表示与文本的字节长度,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch02.xhtml#characters-and-strings-in-memory` - 结论依据:原文以 noodles 与其子串说明拥有和借用,再把合法 UTF-8 作为实现维持的不变量。 - 原始内容:You can think of a &str as being nothing more than a &[u8] that is guaranteed to hold well-formed UTF-8. Likewise, a char is