实现说明
Str Escape Default ASCII 转义以函数 main为骨架。
使用要点
- 每一步打印都带着自己的标签:
escaped:、char by char:、same:。
常见问题
运行「Str Escape Default ASCII 转义」会打印什么?
运行文件本身,escaped:、char by char:、same: 这些前缀各对应一段演示逻辑。
核心语句
let rebuilt: String = raw.chars().flat_map(char::escape_default).collect();
代码示例
fn main() {
let raw = "line one\nline two\t\"quoted\" é";
// escape_default turns control bytes into printable ASCII escapes
let escaped = raw.escape_default().to_string();
println!("escaped: {escaped}");
// each character can be escaped on its own
let rebuilt: String = raw.chars().flat_map(char::escape_default).collect();
println!("char by char: {rebuilt}");
println!("same: {}", escaped == rebuilt);
// escape_debug keeps printable Unicode instead of using code points
let debug = raw.escape_debug().to_string();
println!("debug: {debug}");
println!("debug keeps accent: {}", debug.contains('é'));
// a single escaped newline renders as a two-character sequence
println!("newline escaped: {}", '\n'.escape_default().to_string());
}