Are there any thoughts on adding the ability to construct static CStr
s?
This would be helpful to smooth sending constant strings to a C API. Currently the only ways to do this are:
- Allocate with
CString::new("some string").unwrap().as_c_str()
. This is not possible in const contexts or no_std. - Use byte strings and add the
\0
, communicate with API using.as_ptr()
where needed (sidestepsCStr
entirely) - Use byte strings and add the
\0
, then transmute toCStr
(risky, especially with this discussion) - Use the crate
cstr
, which wraps the first option
It seems like there is opportunity for a builtin macro that mirrors the cstr
crate, but uses a transmute option as needed to work in const contexts.
use std::ffi::cstr;
cstr!("I'm a c string")
I know that some syntax like c"This is a CStr"
has been proposed before, but this is understandably controversial.