-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Description
The str
module doesn't document what escape sequences are valid if any. For example, documentation doesn't state whether this will match a newline as would be expected:
let path = Path::new("test.txt");
let mut file = BufferedReader::new(File::open(&path));
for line in file.lines() {
println!("{}", line.unwrap().as_slice().ends_with("\n"));
}
It also doesn't specify what format these strings are supposed to be written in. Much of the documentation including many escape sequences are documented in the reference but it is difficult (slow) to parse as it is. I think it would be good to have it in std::str
or at least to make the reference more clear. More escape documentation is in libsyntax
. Better escape documentation is in the regex
module (though it's not exactly related).
For what it's worth, Python doesn't seem to document it either (at least, I couldn't find it).
Below is my first shot at what it could look like.
Escapes
\a bell (\x07)
\f form feed (\x0C)
\t horizontal tab
\n new line
\r carriage return
\v vertical tab (\x0B)
\\ backslash
U+0009 (HT) (I don't know what HT is)
\x7F 8-bit hex character code (exactly 2 digits)
\u7FFF 16-bit hex character code (exactly 4 digits)
\U7EEEFFFF 32-bit hex character code (exactly 8 digits)
Raw string literals
r
followed by any number (including zero) of #
's followed by "
and closed in the reverse:
r"raw input\\"; // raw input\\
// What is the purpose of these `#`s?
r#"another"#; // another
r###"3 sharps"###; // 3 sharps
Byte literal
A single ASCII character:
b'T'; // T
b'\''; // ' (escape for single quotes)
Byte string literals
A sequence of ASCII characters:
b"foo"; // foo
b"\"foo\""; // "foo" (escape for `"`)
br##"foo #"# bar"##; // foo #"# bar
Note: I'm not really familiar with all the string formats. I'm just to collect them together so they're more obvious.
@steveklabnik Does it make sense to collect them onto the std::str
page? If so, I can probably submit a PR making something like this but I'm not sure where it should go.