Skip to content

Commit ef089ff

Browse files
committed
Rollup merge of rust-lang#26145 - steveklabnik:gh25853, r=brson
Fixes rust-lang#25853
2 parents 32e96aa + 34e5c24 commit ef089ff

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/doc/trpl/strings.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ let dog = hachiko.chars().nth(1); // kinda like hachiko[1]
117117

118118
This emphasizes that we have to go through the whole list of `chars`.
119119

120+
## Slicing
121+
122+
You can get a slice of a string with slicing syntax:
123+
124+
```rust
125+
let dog = "hachiko";
126+
let hachi = &dog[0..5];
127+
```
128+
129+
But note that these are _byte_ offsets, not _character_ offsets. So
130+
this will fail at runtime:
131+
132+
```rust,should_panic
133+
let dog = "忠犬ハチ公";
134+
let hachi = &dog[0..2];
135+
```
136+
137+
with this error:
138+
139+
```text
140+
thread '<main>' panicked at 'index 0 and/or 2 in `忠犬ハチ公` do not lie on
141+
character boundary'
142+
```
143+
120144
## Concatenation
121145

122146
If you have a `String`, you can concatenate a `&str` to the end of it:

0 commit comments

Comments
 (0)