From eb00e97b2a3136b9e04c8033ac57303a38036421 Mon Sep 17 00:00:00 2001 From: Tanish-Eagle Date: Wed, 30 Nov 2022 20:51:38 +0530 Subject: [PATCH] Edited the iterator.md --- en/src/functional-programing/iterator.md | 48 ++++++++++++------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/en/src/functional-programing/iterator.md b/en/src/functional-programing/iterator.md index 211ab75..eb8d51c 100644 --- a/en/src/functional-programing/iterator.md +++ b/en/src/functional-programing/iterator.md @@ -11,7 +11,7 @@ fn main() { } ``` -In above code, You may consider `for` as a simple loop, but actually it is iterating over a iterator. +In the code above, You may consider `for` as a simple loop, but actually it is iterating over a iterator. By default `for` will apply the `into_iter` to the collection, and change it into a iterator. As a result, the following code is equivalent to previous one: ```rust @@ -23,18 +23,18 @@ fn main() { } ``` -1、🌟 +1.🌟 ```rust,editable /* Refactoring the following code using iterators */ fn main() { let arr = [0; 10]; for i in 0..arr.len() { - println!("{}",arr[i]) + println!("{}",arr[i]); } } ``` -2、 🌟 One of the easiest ways to create an iterator is to use the range notion: `a..b`. +2. 🌟 One of the easiest ways to create an iterator is to use the range notion: `a..b`. ```rust,editable /* Fill in the blank */ fn main() { @@ -55,13 +55,13 @@ pub trait Iterator { fn next(&mut self) -> Option; - // methods with default implementations elided + // Methods with default implementations elided } ``` And we can call the `next` method on iterators directly. -3、🌟🌟 +3.🌟🌟 ```rust,editable /* Fill the blanks and fix the errors. Using two ways if possible */ @@ -75,28 +75,28 @@ fn main() { ``` ## into_iter, iter and iter_mut -In the previous section, we have mentioned that `for` will apply the `into_iter` to the collection, and change it into a iterator.However, this is not the only way to convert collections into iterators. +In the previous section, we have mentioned that `for` will apply the `into_iter` to the collection, and change it into a iterator. However, this is not the only way to convert collections into iterators. -`into_iter`, `iter`, `iter_mut`, all of them can convert an collection into iterator, but in different ways. +`into_iter`, `iter`, `iter_mut`, all of them can convert a collection into iterator, but in different ways. -- `into_iter` cosumes the collection, once the collection has been comsumed, it is no longer available for reuse, because its ownership has been moved within the loop. +- `into_iter` consumes the collection, once the collection has been consumed, it is no longer available for reuse, because its ownership has been moved within the loop. - `iter`, this borrows each element of the collection through each iteration, thus leaving the collection untouched and available for reuse after the loop - `iter_mut`, this mutably borrows each element of the collection, allowing for the collection to be modified in place. -4、🌟 +4.🌟 ```rust,editable /* Make it work */ fn main() { let arr = vec![0; 10]; for i in arr { - println!("{}", i) + println!("{}", i); } println!("{:?}",arr); } ``` -5、🌟 +5.🌟 ```rust,editable /* Fill in the blank */ fn main() { @@ -113,7 +113,7 @@ fn main() { } ``` -6、🌟🌟 +6.🌟🌟 ```rust,editable /* Fill in the blank */ fn main() { @@ -130,7 +130,7 @@ fn main() { ## Creating our own iterator -We can not only create iterators from collections types, but also can create iterators by implementing the `Iterator` trait on our own types. +We can not only create iterators from collection's types, but also can create iterators by implementing the `Iterator` trait on our own types. **Example** ```rust @@ -169,7 +169,7 @@ fn main() { } ``` -7、🌟🌟🌟 +7.🌟🌟🌟 ```rust,editable struct Fibonacci { curr: u32, @@ -208,8 +208,8 @@ The `Iterator` trait has a number of methods with default implementations provid ### Consuming adaptors Some of these methods call the method `next`to use up the iterator, so they are called *consuming adaptors*. -8、🌟🌟 -```rust,edtiable +8.🌟🌟 +```rust,editable /* Fill in the blank and fix the errors */ fn main() { let v1 = vec![1, 2, 3]; @@ -226,10 +226,10 @@ fn main() { ``` -#### collect -Other than converting a collection into an iterator, we can also `collect` the result values into a collection, `collect` will cosume the iterator. +#### Collect +Other than converting a collection into an iterator, we can also `collect` the result values into a collection, `collect` will consume the iterator. -9、🌟🌟 +9.🌟🌟 ```rust,editable /* Make it work */ use std::collections::HashMap; @@ -251,9 +251,9 @@ fn main() { ### Iterator adaptors Methods allowing you to change one iterator into another iterator are known as *iterator adaptors*. You can chain multiple iterator adaptors to perform complex actions in a readable way. -But because **all iterators are lazy**, you have to call one of the consuming adapers to get results from calls to iterator adapters. +But because **all iterators are lazy**, you have to call one of the consuming adapters to get results from calls to iterator adapters. -10、🌟🌟 +10.🌟🌟 ```rust,editable /* Fill in the blanks */ fn main() { @@ -265,7 +265,7 @@ fn main() { } ``` -11、🌟🌟 +11.🌟🌟 ```rust /* Fill in the blanks */ use std::collections::HashMap; @@ -281,7 +281,7 @@ fn main() { #### Using closures in iterator adaptors -12、🌟🌟 +12.🌟🌟 ```rust /* Fill in the blanks */ #[derive(PartialEq, Debug)]