about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2015-06-10 22:07:10 +0530
committerManish Goregaokar <manishsmail@gmail.com>2015-06-10 22:07:10 +0530
commitef089ff70daad26f848efbe89bf1a1d1c1e7226d (patch)
treeef10169a4765e5d63586a84d19b84f7e0ec4cb58
parent32e96aa165d2e3cd7f142a9ed64e38289dc73f1a (diff)
parent34e5c24c5d7b693e043b1e35c137959fdcda19a8 (diff)
downloadrust-ef089ff70daad26f848efbe89bf1a1d1c1e7226d.tar.gz
rust-ef089ff70daad26f848efbe89bf1a1d1c1e7226d.zip
Rollup merge of #26145 - steveklabnik:gh25853, r=brson
Fixes #25853
-rw-r--r--src/doc/trpl/strings.md24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/doc/trpl/strings.md b/src/doc/trpl/strings.md
index 55154036286..7d5c43ea14c 100644
--- a/src/doc/trpl/strings.md
+++ b/src/doc/trpl/strings.md
@@ -117,6 +117,30 @@ let dog = hachiko.chars().nth(1); // kinda like hachiko[1]
 
 This emphasizes that we have to go through the whole list of `chars`.
 
+## Slicing
+
+You can get a slice of a string with slicing syntax:
+
+```rust
+let dog = "hachiko";
+let hachi = &dog[0..5];
+```
+
+But note that these are _byte_ offsets, not _character_ offsets. So
+this will fail at runtime:
+
+```rust,should_panic
+let dog = "忠犬ハチ公";
+let hachi = &dog[0..2];
+```
+
+with this error:
+
+```text
+thread '<main>' panicked at 'index 0 and/or 2 in `忠犬ハチ公` do not lie on
+character boundary'
+```
+
 ## Concatenation
 
 If you have a `String`, you can concatenate a `&str` to the end of it: