std::ascii::escape_default

Function std::ascii::escape_default

pub fn escape_default(c: u8) -> EscapeDefault

Returns an iterator that produces an escaped version of a u8.

The default is chosen with a bias toward producing literals that are legal in a variety of languages, including C++11 and similar C-family languages. The exact rules are:

  • Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
  • Single-quote, double-quote and backslash chars are backslash-escaped.
  • Any other chars in the range [0x20,0x7e] are not escaped.
  • Any other chars are given hex escapes of the form '\xNN'.
  • Unicode escapes are never generated by this function.

Examples

use std::ascii;

let escaped = ascii::escape_default(b'0').next().unwrap();
assert_eq!(b'0', escaped);

let mut escaped = ascii::escape_default(b'\t');

assert_eq!(b'\\', escaped.next().unwrap());
assert_eq!(b't', escaped.next().unwrap());

let mut escaped = ascii::escape_default(b'\r');

assert_eq!(b'\\', escaped.next().unwrap());
assert_eq!(b'r', escaped.next().unwrap());

let mut escaped = ascii::escape_default(b'\n');

assert_eq!(b'\\', escaped.next().unwrap());
assert_eq!(b'n', escaped.next().unwrap());

let mut escaped = ascii::escape_default(b'\'');

assert_eq!(b'\\', escaped.next().unwrap());
assert_eq!(b'\'', escaped.next().unwrap());

let mut escaped = ascii::escape_default(b'"');

assert_eq!(b'\\', escaped.next().unwrap());
assert_eq!(b'"', escaped.next().unwrap());

let mut escaped = ascii::escape_default(b'\\');

assert_eq!(b'\\', escaped.next().unwrap());
assert_eq!(b'\\', escaped.next().unwrap());

let mut escaped = ascii::escape_default(b'\x9d');

assert_eq!(b'\\', escaped.next().unwrap());
assert_eq!(b'x', escaped.next().unwrap());
assert_eq!(b'9', escaped.next().unwrap());
assert_eq!(b'd', escaped.next().unwrap());

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ascii/fn.escape_default.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部