Macro zstr::zstr [−][src]
zstr!() { /* proc-macro */ }
Given a Rust string or byte string literal, this macro
generates an expression of type &'static CStr
that is
properly 0-terminated and ensured not to contain any
internal NUL (0) bytes. The conversion is zero-cost and
it may even become const
in the future, provided that
CStr::from_bytes_with_nul_unchecked()
becomes const
.
Examples:
use zstr::zstr; // Works with Unicode characters and escapes in Rust strings let c_str_1 = zstr!("Hello 🎉"); assert_eq!(c_str_1.to_bytes(), b"Hello \xf0\x9f\x8e\x89"); assert_eq!(c_str_1.to_bytes_with_nul(), b"Hello \xf0\x9f\x8e\x89\x00"); let c_str_2 = zstr!("Hello \u{1F389}"); assert_eq!(c_str_1, c_str_2); let c_str_3 = zstr!(b"hello\x20ASCII"); assert_eq!(c_str_3.to_bytes(), b"hello ASCII"); assert_eq!(c_str_3.to_bytes_with_nul(), b"hello ASCII\x00");
Strings with embedded NUL (zero) bytes are not allowed:
ⓘ
let invalid_1 = zstr!("null here: \x00 is forbidden"); let invalid_2 = zstr!("also at the end: \0"); let invalid_3 = zstr!(b"and in byte \x00 strings too");