知识卡片

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

C字符串与Rust文本契约不同

理解等级:中级

Rust String是带长度的有效UTF-8,可含零字节;C字符串以首个NUL终止,不保证UTF-8。CString提供拥有的C字符串,CStr提供借用视图,传指针前需保证目标存活。as_ptr给只读访问,不能让外部写入或越过终止符边界。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 6. Beyond Standard Rust Accessing C Data from Rust · 查看原书依据

      CString type to hold (owned) strings that need to be interoperable with C, and use the corresponding CStr type when dealing with borrowed string values. The latter type includes the as_ptr() method, which can be used to pass the string’s contents to any FFI function that’s expecting a const char* C string. Note that the const is important: this can’t be used

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第6章《Accessing C Data from Rust》(spine10;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch06.html;u178-ch06-012)。 - 结论依据:原文在“CString”附近的定义、例子或代码支撑本卡核心说明:Rust String是带长度的有效UTF-8,可含零字节;C字符串以首个NUL终止,不保证UTF-8。边界按已读前提解释;示例未执行。 - 原始内容:CString type to hold (owned) strings that need to be interoperable with C, and use the corresponding CStr type when dealing with borrowed string values. The latter type includes the as_ptr() method, which can be used to pass the string’s contents to any FFI function that’s expecting a const char* C string. Note that the const is important: this can’t be used