2 个版本
| 0.1.1 | 2024 年 7 月 22 日 |
|---|---|
| 0.1.0 | 2024 年 7 月 22 日 |
#609 in Rust 模式
每月 248 次下载
43KB
994 行
c8str
此包提供 C8Str 和 C8String 类型,这些类型结合了 Rust 原生 utf-8 字符串和 C 风格的 null 结尾字符串的特性。这些类型保证
- 字符串是有效的 utf-8
- 字符串以 null 终结符结尾
- 字符串在结尾之前不包含任何 null 字节
这两种类型都提供获取对 &str(带有或没有 null 终结符)和 &CStr 的引用或对 *const c_char 的指针的方法。它们解引用到没有 null 终结符的 &str。
c8 宏可以从字符串字面量创建 &C8Str 类型的编译时常量。
C8Str 与 no_std 兼容。C8String 可在 alloc 功能之后使用。
# use c8str::c8;
# use core::ffi::CStr;
assert_eq!(c8!("hello").as_str(), "hello");
assert_eq!(c8!("hello").as_c_str(), CStr::from_bytes_with_nul(b"hello\0").unwrap());
// assert_eq!(c8!("hello").as_c_str(), c"hello")); // from rust 1.77
特性
alloc- 启用C8String类型。这需要标准alloc或std包。std- 为此包的错误类型实现Error特性来自std。隐含alloc。
版本历史
- 0.1.1 - 在 docs.rs 上显示所有功能的文档
- 0.1.0 - 第一个版本