mirror of
https://github.com/google/comprehensive-rust.git
synced 2024-12-12 05:24:12 +02:00
Add Speaker notes string.md (#368)
* Update string.md * Update src/std/string.md Co-authored-by: Martin Geisler <mgeisler@google.com> * Update src/std/string.md Co-authored-by: Martin Geisler <mgeisler@google.com> * Update string.md --------- Co-authored-by: Martin Geisler <mgeisler@google.com>
This commit is contained in:
parent
e616f66593
commit
60aa747aca
@ -27,7 +27,14 @@ fn main() {
|
||||
|
||||
<details>
|
||||
|
||||
* `len()` returns the size of the `String` in bytes, not its length in characters.
|
||||
* `chars()` returns an iterator over the actual characters.
|
||||
* `String::new` returns a new empty string, use `String::with capacity` when you know how much data you want to push to the string.
|
||||
* `String::len` returns the size of the `String` in bytes (which can be different from its length in characters).
|
||||
* `String::chars` returns an iterator over the actual characters. Note that a `char` can be different from what a human will consider a "character" due to [grapheme clusters](https://docs.rs/unicode-segmentation/latest/unicode_segmentation/struct.Graphemes.html).
|
||||
* When people refer to strings they could either be talking about `&str` or `String`.
|
||||
* When a type implements `Deref<Target = T>`, the compiler will let you transparently call methods from `T`.
|
||||
* `String` implements `Deref<Target = str>` which transparently gives it access to `str`'s methods.
|
||||
* Write and compare `let s3 = s1.deref();` and `let s3 = &*s1`;.
|
||||
* `String` is implemented as a wrapper around a vector of bytes, many of the operations you see supported on vectors are also supported on `String`, but with some extra guarantees.
|
||||
* Compare the different ways to inde a Strings by using `s3[i]` and `s3.chars.nth(i).unwrap()` where `i` is in-bound, out-of-bounds, and "on" the flag unicode character.
|
||||
|
||||
</details>
|
||||
|
Loading…
Reference in New Issue
Block a user