Make iterators 10X better with itertools

preview_player
Показать описание
Today we are learning about the itertools crate and how you can use it to make your iterators more powerful!

Рекомендации по теме
Комментарии
Автор

To convert between Results and Options within the same iterator combinator, you can use flatten() and flat_map() :
I also prefer using Try_from() instead of Try_into() in oder to avoid the turbofish.
When combining these two techniques, you can often avoid long closures, and sometimes just pass the function directly to the iterator combinator using the functionnal style.

log.flatten() // Only keeps the happy paths : turns Ok(T) into T
.map(|s| s.as_str())
// Equivalent to .map().flatten() chained together and uses functionnal syntax

BlackSharkfr
Автор

A more efficient version would be calling sorted() first then dedup() rather than unique(). This would produce the code that would be exactly like how you would write it manually: put everything into a vec first then sort it then remove any duplicates linearly in a single pass with O(1) space use.

EgnachHelton
Автор

Iterators are so powerful; wish people used them more. Back in Kotlin, I did some crazy cool stuff with them — mostly crazy 😅. They're just fun!

solmateusbraga
Автор

Just wanted to thank you! All your videos have a great sound, like no mouth noises or lip-smack, this is really important to me. Thank you again!

mgyzppu
Автор

Dude this video is really good.

You mentioned a lot of stuff that I get hung up on when writing rust.

Thanks!

Thisguyrocks
Автор

just came here to thank your efforts for making cheat sheet. it saved my time and increased my pace to learn the lang

giantakeshi
Автор

Doesn't chain() from std already provide the same functionality marge() gives?

m.stradus
Автор

Rubyists like me love that Rust can iterate over collections this easily.

billhurt
Автор

You could use the identity function from std::convert to annotate the type

wuerges
Автор

What happens if one date is corrupted? Whole line is dismissed or just the date is flagged as invalid?

adriankal
Автор

When calling sorted and unique, there two itération of the vec or there are combine in one iteration?

wiiznokes
Автор

Which visual studio extension you demo to import use? Thanks in advance

leighwanstead
Автор

The single most useful method in itertools is "format_with". Printing a formated string in rust with iterators can be very annoying if you want to avoid repeatedly allocating and deallocating memory for String. This method has a weird syntax but very useful to help you avoid allocations.

EgnachHelton
Автор

No one:
literally no one:
me who's portuguese looking at the thumbnail: EU SOU O BOB CONSTRUTOR, È O BOB TRABALHADOR

coffee-is-power
Автор

Itertools is great. Perhaps `rstest` next?

scheimong
Автор

I love functional approaches, but can't get over the inefficiency here 🐴
All those String allocations, and the HashSet inside unique.
I'd either insert directly into a BTreeMap or into a HashSet and consume it into a Vec and sort it.
(custom Hash implementation that only looks at timestamp, but Ord implementation that includes the strings? :v)

qmster
Автор

At 0:30 I thought I spotted a bug with for i in vec and I think you wanted for i in iter. I put the code in the playground and no go. Vec doesn't implement the Copy trait rustc says. Oops my bad. Not a good example though hiding the into_iter context and move. If you try and access the vec after the loop boom!. Note a &vec is probably better used in the example.

JohnPywtorak
Автор

You should probably call sorted() before you call unique()

kwinzman
Автор

CITIES WERE NOT SUPPOSED TO BE IN THE SKIES‼️‼️

PipeyardCentipede
Автор

personally i disagree with "first class iterator support". we can talk about that label when geneators become a stable feature.

Max-mxyc