mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-22 15:57:46 +02:00
Avoid fixed byte offsets in strings.md (#1963)
As discovered during #1961, fixed byte offsets tend to break translations because the translated strings can end up having a character on the boundary where we slice.
This commit is contained in:
parent
9ba6c8f8df
commit
9cc3e9c5ed
@ -13,6 +13,9 @@ We can now understand the two string types in Rust:
|
|||||||
- `&str` is a slice of UTF-8 encoded bytes, similar to `&[u8]`.
|
- `&str` is a slice of UTF-8 encoded bytes, similar to `&[u8]`.
|
||||||
- `String` is an owned, heap-allocated buffer of UTF-8 bytes.
|
- `String` is an owned, heap-allocated buffer of UTF-8 bytes.
|
||||||
|
|
||||||
|
<!-- Avoid using fixed integers when slicing since this breaks
|
||||||
|
translations. Using the length of s1 and s2 is safe. -->
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
let s1: &str = "World";
|
let s1: &str = "World";
|
||||||
@ -23,7 +26,7 @@ fn main() {
|
|||||||
s2.push_str(s1);
|
s2.push_str(s1);
|
||||||
println!("s2: {s2}");
|
println!("s2: {s2}");
|
||||||
|
|
||||||
let s3: &str = &s2[6..];
|
let s3: &str = &s2[s2.len() - s1.len()..];
|
||||||
println!("s3: {s3}");
|
println!("s3: {s3}");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user