Macro c_utf8::c_utf8 [−][src]
macro_rules! c_utf8 { ($s:expr) => { ... }; }
Creates a &'static CUtf8
from a native Rust str
string literal, making it much easier to work with C APIs that are strict
about encoding input as UTF-8.
Usage
Although the input string can have a 0 byte, it is highly recommended to not have one. This is because C APIs will only work with the memory up to the first 0 byte. In the future, it will be very likely be a hard error to have a 0 byte within the string literal.
Examples
The resulting string will always end with a 0 byte:
#[macro_use] extern crate c_utf8; fn main() { let string = c_utf8!("Hello!"); let bytes = [72, 101, 108, 108, 111, 33, 0]; assert_eq!(string.as_bytes_with_nul(), &bytes); }
The macro can even be evaluated within a constant expression. This allows
for having instances of types with &'static CUtf8
fields.
static APP_NAME: &CUtf8 = c_utf8!(env!("CARGO_PKG_NAME")); assert_eq!(APP_NAME.as_str_with_nul(), "c_utf8\0");