From 6744822454a2ba3e9ed75fdb2acc1314516701c5 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Mon, 24 Apr 2023 14:46:38 -0400 Subject: [PATCH] Minor fixes (#579) * order the Cat and Dog correctly in the vec * make example editable * patterns: capture vs. constant * clarify notes on indexing a string --- src/pattern-matching/destructuring-structs.md | 4 ++++ src/std/string.md | 4 +++- src/traits/trait-objects.md | 6 +++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/pattern-matching/destructuring-structs.md b/src/pattern-matching/destructuring-structs.md index ceaef1db..67fbb211 100644 --- a/src/pattern-matching/destructuring-structs.md +++ b/src/pattern-matching/destructuring-structs.md @@ -9,4 +9,8 @@ You can also destructure `structs`: * Change the literal values in `foo` to match with the other patterns. * Add a new field to `Foo` and make changes to the pattern as needed. +* The distinction between a capture and a constant expression can be hard to + spot. Try changing the `2` in the second arm to a variable, and see that it subtly + doesn't work. Change it to a `const` and see it working again. + diff --git a/src/std/string.md b/src/std/string.md index c44fb7c1..9128ee13 100644 --- a/src/std/string.md +++ b/src/std/string.md @@ -35,6 +35,8 @@ fn main() { * `String` implements `Deref` 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 index a `String` by using `s3[i]` and `s3.chars().nth(i).unwrap()` where `i` is in-bound, out-of-bounds, and "on" the flag Unicode character. +* Compare the different ways to index a `String`: + * To a character by using `s3.chars().nth(i).unwrap()` where `i` is in-bound, out-of-bounds. + * To a substring by using `s3[0..4]`, where that slice is on character boundaries or not. diff --git a/src/traits/trait-objects.md b/src/traits/trait-objects.md index 488cf903..584c819c 100644 --- a/src/traits/trait-objects.md +++ b/src/traits/trait-objects.md @@ -2,7 +2,7 @@ Trait objects allow for values of different types, for instance in a collection: -```rust +```rust,editable trait Pet { fn name(&self) -> String; } @@ -27,8 +27,8 @@ impl Pet for Cat { fn main() { let pets: Vec> = vec![ - Box::new(Dog { name: String::from("Fido") }), Box::new(Cat), + Box::new(Dog { name: String::from("Fido") }), ]; for pet in pets { println!("Hello {}!", pet.name()); @@ -80,4 +80,4 @@ Memory layout after allocating `pets`: println!("{}", std::mem::size_of::>()); ``` - \ No newline at end of file +