知识卡片

普通读书笔记卡 · 6074.h.2

无检查接口仍有安全前提

理解等级:中级

String::from_utf8检查字节并用Result报告非法UTF-8;unsafe的from_utf8_unchecked则要求调用者保证UTF-8有效。违反它可能导致内存不安全,不能当作只会稍后panic的“不会失败”版本。安全性前提与业务错误结果是不同层次,不能用不返回Result推断可随意调用。

纠正依据:String官方安全说明,2026-10-27查阅。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 3. Concepts Item 18: Don’t panic · 查看原书依据

      String::from_utf8_unchecked and String::from_utf8 entrypoints in the standard library are an example of the latter (although in this case, the panics are actually deferred to the point where a String constructed from invalid input gets used). Assuming that you are trying to comply with the advice in this Item, there are a few things to bear in mind. The firs

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Item 18: Don’t panic》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u106-ch03-044)。 - 结论依据:原文在“String::from_utf8_unchecked”附近的定义、例子或代码支撑本卡核心说明:String::from_utf8检查字节并用Result报告非法UTF-8;unsafe的from_utf8_unchecked则要求调用者保证UTF-8有效。边界按已读前提解释;示例未执行。 - 原始内容:String::from_utf8_unchecked and String::from_utf8 entrypoints in the standard library are an example of the latter (although in this case, the panics are actually deferred to the point where a String constructed from invalid input gets used). Assuming that you are trying to comply with the advice in this Item, there are a few things to bear in mind. The firs