是否有'stringify'宏的字节等价?

时间:2017-02-13 08:08:00

标签: macros rust

Rust有一个stringify!宏来将表达式作为字符串。 有没有办法获得输出字节的等效功能?

好像表达式被写为字节字符串文字,例如:b"some text"

使用宏而不是str.as_bytes()的原因是转换函数无法用于构造const值。
请参阅:How to construct const integers from literal byte expressions?
为什么你可能想要使用这个宏。

1 个答案:

答案 0 :(得分:3)

如果您使用夜间Rust(自1.28.0-夜间,2018-05-23),您可以启用const_str_as_bytes功能,将as_bytes()转换为const功能。

#![feature(const_str_as_bytes)]

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

Demo

相关问题