| Crates.io | stack-cstr |
| lib.rs | stack-cstr |
| version | 0.2.0 |
| created_at | 2025-08-21 07:15:09.851856+00 |
| updated_at | 2025-12-24 10:56:52.570456+00 |
| description | High-performance stack-to-heap C string creation for Rust with FFI support |
| homepage | |
| repository | https://github.com/fxdmhtt/stack-cstr |
| max_upload_size | |
| id | 1804398 |
| size | 39,075 |
stack_cstr is a high-performance Rust library for creating C-compatible strings (&CStr) efficiently.
It uses a stack buffer for short strings to avoid heap allocation, and automatically falls back to heap allocation for longer strings.
The resulting strings are safe to pass to FFI functions.
format_args! style formattingCArrayString<128> for easy FFI usagecstr!()use std::ffi::CStr;
use stack_cstr::cstr;
// Create a C-compatible string
let s = cstr!("Pi = {:.2}", 3.14159);
assert_eq!(s.as_c_str().to_str().unwrap(), "Pi = 3.14");
unsafe {
// Pass to FFI as *const c_char
let ptr = s.as_ptr();
assert_eq!(CStr::from_ptr(ptr).to_str().unwrap(), "Pi = 3.14");
}