[][src]Macro byte_strings::as_bytes

macro_rules! as_bytes {
    (
        $str_literal:expr
    ) => { ... };
}

Evaluates the input string literal as a byte string literal.

Hence the macro evaluates to the type &'static [u8; N] (where N is the total number of bytes), which can also "be seen as" (coerce to) a static byte slice (i.e., &'static [u8]).

The input can be a byte string literal, in which case it is a no-op.

Example

This code runs with edition 2018
use ::byte_strings::as_bytes;

let bytes = as_bytes!("Hello, World!");
assert_eq!(bytes, b"Hello, World!");

Macro expansion:

For those curious, as_bytes!("Hello, World!") expands to:

{
    const __byte_strings__as_bytes: &'static [u8; 13usize] = b"Hello, World!";

    __byte_strings__as_bytes
}

This trick is needed to circumvent the current restriction of procedural macros being able to expand to items only.