[英]Convert an ascii string literal to &[u8] in Rust?
How do you convert an ascii string literal (say, "123 458") into a &[u8]
slice in rust?如何将 ascii 字符串文字(例如“123 458”)转换为
&[u8]
的&[u8]
切片? Rust has 6? Rust 有 6 个? 7?
7? string types, and there is almost no uniformity in what you call to convert between them.
字符串类型,并且您调用它们之间的转换几乎没有统一性。
I'm expecting it to be something along the lines of:我期待它是类似的东西:
let array:&[u8] = Ascii("123 456").into_bytes().as_slice().givemetheeffingbufferalready() as &[u8];
Please take it on faith that I actually need to do this;请相信我确实需要这样做; I am writing test cases for some file parser code.
我正在为一些文件解析器代码编写测试用例。
After much digging through other related answers, and getting corrected by the nightly version of the compiler, I found out it is trivial;经过大量挖掘其他相关答案,并通过编译器的夜间版本得到纠正后,我发现这很简单; you just add a 'b' to your literal:
您只需在文字中添加一个“b”:
let array:&[u8] = b"123 456";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.