From d603faca5625a68ae3fd36a95b1668ee996b4231 Mon Sep 17 00:00:00 2001 From: Nicole L Date: Fri, 7 Feb 2025 11:04:32 -0800 Subject: [PATCH] Add link to `GridRefIter` example (#2633) --- src/iterators/intoiterator.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/iterators/intoiterator.md b/src/iterators/intoiterator.md index 631ca0bd83f4..2ecee30772b3 100644 --- a/src/iterators/intoiterator.md +++ b/src/iterators/intoiterator.md @@ -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 `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 @@ -85,3 +86,5 @@ vector. Use `for e in &some_vector` instead, to iterate over references to elements of `some_vector`. + +[1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=947e371c7295af758504f01f149023a1