add zh/string.md

This commit is contained in:
sunface
2022-02-27 13:33:05 +08:00
parent f7099ea297
commit 072de76808
3 changed files with 264 additions and 33 deletions

View File

@ -140,28 +140,10 @@ fn main() {
}
```
🌟🌟 Sometimes there are just too many characters that need to be escaped or it's just much more convenient to write a string out as-is. This is where raw string literals come into play.
🌟🌟🌟 Sometimes there are just too many characters that need to be escaped or it's just much more convenient to write a string out as-is. This is where raw string literals come into play.
```rust,editable
fn main() {
let raw_str = r"Escapes don't work here: \x3F \u{211D}";
println!("{}", raw_str);
// If you need quotes in a raw string, add a pair of #s
let quotes = r#"And then I said: "There is no escape!""#;
println!("{}", quotes);
// If you need "# in your string, just use more #s in the delimiter.
// You can use up to 65535 #s.
let longer_delimiter = r###"A string with "# in it. And even "##!"###;
println!("{}", longer_delimiter);
}
```
🌟🌟🌟
```rust,editable
fn main() {
let raw_str = r"Escapes don't work here: \x3F \u{211D}";
// modify below line to make it work
@ -255,18 +237,6 @@ fn main() {
}
```
🌟🌟
```rust,editable
use utf8_slice;
fn main() {
let s = "The 🚀 goes to the 🌑!";
let rocket = utf8_slice::slice(s, 4, 5);
}
```
#### utf8_slice
You can use [utf8_slice](https://docs.rs/utf8_slice/1.0.0/utf8_slice/fn.slice.html) to slice UTF8 string, it can index chars instead of bytes.