File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -117,6 +117,30 @@ let dog = hachiko.chars().nth(1); // kinda like hachiko[1]
117
117
118
118
This emphasizes that we have to go through the whole list of ` chars ` .
119
119
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
+
120
144
## Concatenation
121
145
122
146
If you have a ` String ` , you can concatenate a ` &str ` to the end of it:
You can’t perform that action at this time.
0 commit comments