Is there a byte equivalent to the 'stringify' macro?

Rust has a stringify! macro stringify! to get the expression as a string. Is there a way to get equivalent functionality that outputs bytes instead?

As if the expression was written as a string literal of a byte, for example: b"some text" .


The reason for using the macro instead of str.as_bytes() is that the conversion functions cannot be used to create const values. See this question to find out why you can use this macro .

+6
source share
1 answer

If you use nightly Rust (starting from 1.28.0-nightly, 2018-05-23), you can include the const_str_as_bytes function, which turns as_bytes() into a const function.

 #![feature(const_str_as_bytes)] fn main() { const AAA: &[u8] = stringify!(aaa).as_bytes(); println!("{:?}", AAA); // [97, 97, 97] } 

( Demo )

+3
source

Source: https://habr.com/ru/post/1014931/


All Articles