Skip to content
0:00 / 0:00

#rust#iterators#traits

Crust of Rust: Iterators

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube
  • Recommended by 4 people: 1 on Lobsters and 3 in engineering communities.
  • A classic. People kept recommending it from 2021 to 2025.
  • Presented by Jon Gjengset

Intermediate. Assumes working knowledge.

126K views6 years ago4 recommendations

Programming languagesRust

Jon Gjengset explains Rust iterators and trait bounds by re-implementing the flatten iterator method from the standard library.

In this third Crust of Rust video, we cover iterators and trait bounds, by re-implementing the "flatten" Iterator method from the standard library. As part of that, we cover some of the weirder trait bounds that are required, including what's needed to extend the implementation to support backwards iteration. You can find the final code at gist.github.com/jonhoo/dd63b720fa4a220ea845a77e2d75831e. Introduction The Iterator trait The IntoIterator trait Generic traits vs associated types Provided Iterator methods Iterator::flatten Associated items of generics in bounds Why must O as Trait be bracketed? Flattening more than two levels First attempt at Flatten::next Two elements in inner iterator Two elements in outer iterator Simplifying with ? Storing the inner iterator Trait bound syntax Finishing corrected next Ergonomics with IntoIterator DoubleEndedIterator First draft of DEI implementation Testing double-ended iteration Iterating from both ends in parallel The cost of two cursors Iterators are like Futures Calling next and next_back concurrently ref in patterns Why not flatten first, then iterate More ref in patterns Deeper flattening FlatMap Ergonomics through extension traits Sized in traits You can watch the live version with comments at youtube.com/watch?v=pqHWEsXamYg

Why it's here

4 people recommend this

What people say. Highly recommended as an excellent, deep-dive explanation of how iterators and trait bounds work in Rust.

bitfieldon Lobsters • Recommends

Crust of Rust: Iterators (2020)

u/JonhooRust for Rustaceansin r/rust • Recommends

On “Uncovered Intermediate Topics”

Are you looking for something beyond link to this video and link to this video ?

u/Mikeman89in r/rust • Recommends

On “Most complex type signature?”

It’s an old video but I find he explains it well as part of this video even if the intent is iterators link to this video I recommend watching the entire crust of rust playlist too it’s amazing and still relevant even if some videos are old.

u/hallb1016in r/rust • Recommends

On “Can you compose std::io::Lines with std::iter::count?”

This is a good opportunity to learn how iterators work in Rust. I recommend Jon Gjengset's Crust of Rust: Iterators video to get a good understanding of how they work, the documentation for the Iterator trait for a briefer overview, and the Rust by Example iterators page. The `std::io::lines` iterator returns `Some(Result<String, std::io::Error>)` while there are still lines in the file, and `None` if the end of the file is reached. What this means is that if there is an error reading the file, the iterator does not return `None`; it instead returns `Some(Err)` \-- otherwise, the error information cannot be propagated to the caller. `count` essentially loops over all the elements of the…