知识卡片
文本类型匹配外部边界
常规文本用 String 或 &str 并获得 UTF-8 保证。处理文件路径用 PathBuf 或 &Path;需要保存可能非 UTF-8 的系统参数和环境变量内容时,用 OsString 或 &OsStr。二进制协议以字节序列承接;C 接口约定由零字节结束时,用 CString 或 &CStr 保留这一约定。
关联阅读:[[字符串类型维护编码约束]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Fundamental Types 外部接口决定文本的表示 · 查看原书依据
Rust guarantees that strings are valid UTF-8. Sometimes a program really needs to be able to deal with strings that are not valid
参考来源
- 位置:《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#other-string-like-types`
- 结论依据:原文从可能不是 Unicode 的文件名引入问题,按日常文本、文件系统、系统参数、二进制和 C 接口给出配套类型。
- 原始内容:Rust guarantees that strings are valid UTF-8. Sometimes a program really needs to be able to deal with strings that are not valid