forked from rust-lang/rustlings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iterators4.rs was particularly instructive. I started out by searching for the initial C recursion and for loop versions and tried to use a range iteration but couldn't figure the syntax. So I created the first 7 answers and then used hint. I then seached for: how to use iterator for side effects only https://www.google.com/search?q=how+to+use+iterator+for+side+effects+only+in+rust And saw this: rust-lang/rust#44546 (comment) And: rust-lang/rust#44546 (comment) And with that I implemented Answer 8 But still wasn't happy as I felt there must be a better way where I didn't need to throw away things. So I looking at the iterator documentation: https://doc.rust-lang.org/std/iter/trait.Iterator.html I saw "fold" and low and behold that was exactly what I needed! Then I saw "fold_first" and it was "simpler" as the first value in the range is the initial value for the accumulator so fold_first only has one parameters not two. But fold_first returns an Option which means I need to return the unwrap(). Anyway both Answers 9 and 10 are simpler then any other solution! I do wonder which is fastest.
- Loading branch information
1 parent
8bab9fe
commit b58936a
Showing
6 changed files
with
233 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters