环境
- Rust 1.56.1
- VSCode 1.61.2
概念
参考:https://doc.rust-lang.org/rust-by-example/std/str.html
示例
在字符串前加上一个 b
来表示。
main.rs
use std::str;
fn main() {
let bytestring = b"this is a byte string";
println!("A byte string: {:?}", bytestring);
let escaped = b"\x52\x75\x73\x74 as bytes";
println!("Some escaped bytes: {:?}", escaped);
let raw_bytestring = br"\u{211D} is not escaped here";
println!("{:?}", raw_bytestring);
if let Ok(my_str) = str::from_utf8(raw_bytestring) {
println!("And the same as text: '{}'", my_str);
}
let _quotes = br#"You can also use "fancier" formatting, \
like with normal raw strings"#;
let shift_jis = b"\x82\xe6\x82\xa8\x82\xb1\x82\xbb"; // "ようこそ" in SHIFT-JIS
match str::from_utf8(shift_jis) {
Ok(my_str) => println!("Conversion successful: '{}'", my_str),
Err(e) => println!("Conversion failed: {:?}", e),
};
}
总结
了解了 Rust 中字节数组的定义和使用方式。