Skip to content

Commit b7faf6d

Browse files
committed
Update Indexing
1 parent e6c0efa commit b7faf6d

4 files changed

Lines changed: 21 additions & 23 deletions

File tree

content/en/docs/c2.borrowing.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,17 @@ fn main() {
196196
// 💯 Using the `move` keyword forces the closure to take ownership: `let greet = move || println!("Hello, {name}!");`
197197
```
198198

199-
Using an index `[n]` on collections like `Vec` or `HashMap` involves an implicit shared borrow of the element. Example: The compiler effectively converts `a[0]` into `*a.index(0)`.
199+
Accessing an element from a slice or collection like `Vec` or `HashMap` for read-only access involves the [`Index` trait](https://doc.rust-lang.org/std/ops/trait.Index.html). The compiler implicitly converts `a[0]` into `*a.index(0)`, which involves an implicite shared borrow of the collection.
200200

201201
```rust
202-
fn main() {
203-
let a = vec![1, 2, 3]; // Vector of a Copy type
204-
let b = vec!["Apple".to_string(), "Pear".to_string()]; // Vector of a non-Copy/ move type
205-
206-
// 💡 a[0] and b[0] are references to the relevant elements of the vectors
202+
// Two concepts in the example: 1. Implicite shared reference; 2. Assigning Copy types/ non-Copy types to a new variable.
207203

208-
let c = a[0]; // A copy of a[0] is assigned to `c: i32`
204+
fn main() {
205+
let a = vec![1, 2, 3]; // 💡 contains Copy types
206+
let b = vec!["Apple".to_string(), "Pear".to_string()]; // 💡 contains non-Copy/ move types
209207

210-
// Without .clone(), b[0] would attempt to move into `d: String`
211-
// ⭐️ Since this would destroy the original element inside the vector, the Rust borrow checker will not let it compile
208+
let c = a[0]; // a[0] access the element via a shared reference + Copy the element into `c: i32`
209+
// b[0] access the element via a shared reference + manually call `.clone()` as non-Copy types move/ destroy the original data
212210
let d = b[0].clone();
213211

214212
println!("{c} {d}");
@@ -266,6 +264,7 @@ fn borrow_struct_to_assign(p: &Person) {
266264
println!("{a} {b}");
267265
}
268266

267+
// ⭐️ Each reference is scoped to the function call.
269268
// 👨‍🏫 Try remove `.clone()`. The borrow checker will not let it compile.
270269
// 💥 cannot move out of `p.name` which is behind a shared reference
271270
```
@@ -325,7 +324,7 @@ fn main() {
325324
}
326325
```
327326

328-
Using an index `[n]` on collections like `Vec` or `HashMap` to modify an element involves an implicit mutable borrow. Example: The compiler effectively converts `a[0]` into `*a.index_mut(0)`.
327+
Accessing an element from a slice or collection like `Vec` for mutable access involves the [`IndexMut` trait](https://doc.rust-lang.org/std/ops/trait.IndexMut.html). The compiler implicitly converts `a[0]` into `*a.index_mut(0)`, which involves an implicit mutable borrow of the collection.
329328

330329
```rust
331330
fn main() {

0 commit comments

Comments
 (0)