22 个版本 (13 个稳定版)
1.3.2 | 2024 年 8 月 3 日 |
---|---|
1.3.1 | 2021 年 5 月 29 日 |
1.3.0 | 2020 年 4 月 6 日 |
1.2.3 | 2018 年 12 月 4 日 |
0.3.2 | 2017 年 3 月 21 日 |
#139 在 并发 中排名
每月 1,024 次下载
在 5 个 Crates 中使用 (2 个直接使用)
32KB
378 行代码(不包括注释)
线程安全、引用计数的空终止字符串。
这个crate提供了一个存储不可变字符串的空间高效机制。最好的说明方式是查看替代方案
// &str:
// - content must be known at compile time
// + can be shared between threads
// + space overhead is 2*usize (fat pointer to the string)
let s = "foobar";
// String
// + can be created at runtime
// - cannot be shared between threads (except with Clone)
// - space overhead of 3*usize (Vec capacity and len + pointer to bytes)
// - accessing string requires two pointer derefs
let s = format!("foobar");
// CString:
// * mostly same as String
// * space overhead is 2*usize (uses Box<[u8]> internally)
// - cannot contain internal \0 bytes
use std::ffi::CString;
let s = CString::new("foobar").unwrap();
// CStr:
// + space overhead is just the pointer (1*usize)
// - hard to construct
// - cannot contain internal \0 bytes
// - generally cannot be shared between threds (lifetime usually not 'static)
use std::ffi::CStr;
let s: &CStr = &*s;
// Arc<String>:
// + can be created at runtime
// + can be shared between threads
// - space overhead is 7*usize:
// - pointer to Arc
// - weak count
// - strong count
// - pointer to String
// - String overhead (3*usize)
use std::sync::Arc;
let s = ArcCStr::try_from(format!("foobar")).unwrap();
// Arc<str>:
// + can be created at runtime
// + can be shared between threads
// - space overhead is 4*usize:
// - pointer to Arc
// - str length
// - weak count
// - strong count
let s: Arc<str> = Arc::from("foobar");
// Arc<CStr>:
// + can be created at runtime
// + can be shared between threads
// - space overhead is 4*usize:
// - pointer to Arc
// - CStr length
// - weak count
// - strong count
// - cannot contain internal \0 bytes
let s: Arc<CStr> = Arc::from(CStr::from_bytes_with_nul(b"foobar\0").unwrap());
// ArcCStr:
// + can be created at runtime
// + can be shared between threads
// - space overhead is 2*usize (pointer + strong count)
// - cannot contain internal \0 bytes
use arccstr::ArcCStr;
let s = ArcCStr::try_from("foobar").unwrap();
查看 ArcCStr
文档以获取更多详细信息。
依赖
~110–345KB