macro_rules! c_str {
(
$($literal:expr),* $(,)?
) => { ... };
}
Expand description
Converts into a valid [C string] at compile time (no runtime cost)
This macro takes any number of comma-separated byte string literals, or string literals, and evaluates to (a static reference to) a [C string] made of all the bytes of the given literals concatenated left-to-right, with an appended null byte terminator.
Hence the macro evaluates to the type &'static ::core::ffi::CStr
.
§Example
ⓘ
use ::byte_strings::c_str;
assert_eq!(
c_str!("Hello, ", "World!"),
::std::ffi::CStr::from_bytes_with_nul(b"Hello, World!\0").unwrap(),
)
§Compilation error
For the [C string] to be what should be expected, the arguments cannot contain any null byte. Else the compilation will fail.
§Counter example
ⓘ
// error: input literals cannot contain null bytes
let hello_w = c_str!("Hello, ", "W\0rld!");
§Macro expansion:
const _: &str = stringify! {
c_str!("Hello, ", "World!")
expands to
unsafe {
::std::ffi::CStr::from_bytes_with_nul_unchecked(b"Hello, World!\0")
}