Skip to content

Commit

Permalink
Add link to GridRefIter example (#2633)
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison authored Feb 7, 2025
1 parent c05f0b6 commit d603fac
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/iterators/intoiterator.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ The example iterates over all combinations of x and y coordinates.
Try iterating over the grid twice in `main`. Why does this fail? Note that
`IntoIterator::into_iter` takes ownership of `self`.

Fix this issue by implementing `IntoIterator` for `&Grid` and storing a
reference to the `Grid` in `GridIter`.
Fix this issue by implementing `IntoIterator` for `&Grid` and creating a
`GridRefIter` that iterates by reference. A version with both `GridIter` and
`GridRefIter` is available [in this playground][1].

The same problem can occur for standard library types: `for e in some_vector`
will take ownership of `some_vector` and iterate over owned elements from that
vector. Use `for e in &some_vector` instead, to iterate over references to
elements of `some_vector`.

</details>

[1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=947e371c7295af758504f01f149023a1

0 comments on commit d603fac

Please sign in to comment.