mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-03-20 06:21:09 +02:00
Add link to GridRefIter
example (#2633)
This commit is contained in:
parent
c05f0b6f02
commit
d603faca56
@ -76,8 +76,9 @@ 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
|
Try iterating over the grid twice in `main`. Why does this fail? Note that
|
||||||
`IntoIterator::into_iter` takes ownership of `self`.
|
`IntoIterator::into_iter` takes ownership of `self`.
|
||||||
|
|
||||||
Fix this issue by implementing `IntoIterator` for `&Grid` and storing a
|
Fix this issue by implementing `IntoIterator` for `&Grid` and creating a
|
||||||
reference to the `Grid` in `GridIter`.
|
`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`
|
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
|
will take ownership of `some_vector` and iterate over owned elements from that
|
||||||
@ -85,3 +86,5 @@ vector. Use `for e in &some_vector` instead, to iterate over references to
|
|||||||
elements of `some_vector`.
|
elements of `some_vector`.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
[1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=947e371c7295af758504f01f149023a1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user