Make iterators 10X better with itertools

  Рет қаралды 35,665

Let's Get Rusty

Let's Get Rusty

Жыл бұрын

Today we are learning about the itertools crate and how you can use it to make your iterators more powerful!
FREE Rust Cheat Sheet: letsgetrusty.com/cheatsheet
Code: github.com/letsgetrusty/itert...

Пікірлер: 51
@letsgetrusty
@letsgetrusty Жыл бұрын
📝Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@BlackSharkfr
@BlackSharkfr Жыл бұрын
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()) .flat_map(ApacheLogEntre::try_from) // Equivalent to .map().flatten() chained together and uses functionnal syntax
@solmateusbraga
@solmateusbraga Жыл бұрын
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!
@kevin5475845
@kevin5475845 Жыл бұрын
i used to use them a lot in c# and seems pretty powerful here too. so far i'm a bit more of a newbie in rust
@briggsmiller7095
@briggsmiller7095 Жыл бұрын
I’ve been a Kotlin dev since 1.0. Had been doing Java since 1999. My only goal in my career now is to move to Rust. I want off this JVM (and all the awful legacy code that comes with it!)
@kevin5475845
@kevin5475845 Жыл бұрын
@@briggsmiller7095 nice, i used to program in java. first language i learnt and switched to c# during visual studio 2008 or so more when they stopped supporting J#. went to J# in between
@heavenstone3503
@heavenstone3503 Жыл бұрын
@@kevin5475845 It's funny you say that because i think C# iterators are good but ultimately fall very short of what you can do in Rust At least in my Rust dev doing some C# opinion
@georgehelyar
@georgehelyar Жыл бұрын
C# linq is great, but it's not zero cost. Rx (IObservable) and async streams (IAsyncEnumerable) are also great. I love the C# language too, but once you get to a certain level of production software, the runtime is just an exercise in fighting a garbage collector while keeping the memory usage low enough for a docker container. This is where rust really wins for me.
@EgnachHelton
@EgnachHelton Жыл бұрын
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.
@ShiyalaKohny
@ShiyalaKohny Жыл бұрын
Not necessarily, if you’re working with data that contains lots of duplicates, the minor space optimization of dedup would be offset by the added elements in the sort phase. Reduced time complexity may or may not be more important than reduced space complexity, depends on use case
@aperson4051
@aperson4051 Жыл бұрын
​@@ShiyalaKohnythis guy sorts!
@ShiyalaKohny
@ShiyalaKohny 11 ай бұрын
@@aperson4051 I’m not sure I get your point. I’m saying that *because* he sorts, he should dedup before sorting, which saves time and is therefore more efficient (I can provide benchmarks to support that)
@aperson4051
@aperson4051 11 ай бұрын
@@ShiyalaKohny it's a meme, giving you praise for your sorting knowledge. Nothing serious!
@kwinzman
@kwinzman 8 ай бұрын
You probably want to first sort and then dedup because that will be faster because similar items will be next to each other.
@user-mg6yz6pp7u
@user-mg6yz6pp7u Жыл бұрын
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!
@Thisguyrocks518
@Thisguyrocks518 Жыл бұрын
Dude this video is really good. You mentioned a lot of stuff that I get hung up on when writing rust. Thanks!
@giantakeshi8122
@giantakeshi8122 Жыл бұрын
just came here to thank your efforts for making cheat sheet. it saved my time and increased my pace to learn the lang
@m.stradus
@m.stradus Жыл бұрын
Doesn't chain() from std already provide the same functionality marge() gives?
@billhurt3644
@billhurt3644 Жыл бұрын
Rubyists like me love that Rust can iterate over collections this easily.
@fabienpenso
@fabienpenso Жыл бұрын
Agreed. All my rubyist friends love it as well.
@gpcureton
@gpcureton Ай бұрын
Thanks, this video is a banger.
@wuerges
@wuerges Жыл бұрын
You could use the identity function from std::convert to annotate the type
@flogginga_dead_horse4022
@flogginga_dead_horse4022 Жыл бұрын
super useful!
@GlobalYoung7
@GlobalYoung7 Жыл бұрын
thank you ❤
@paulobitfranca
@paulobitfranca 6 ай бұрын
very cool !!!!
@TheRedbeardster
@TheRedbeardster Жыл бұрын
Great!
@EgnachHelton
@EgnachHelton Жыл бұрын
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.
@coffee-is-power
@coffee-is-power Жыл бұрын
No one: literally no one: me who's portuguese looking at the thumbnail: EU SOU O BOB CONSTRUTOR, È O BOB TRABALHADOR
@leftyhero147
@leftyhero147 Жыл бұрын
kkkkkkk
@leighwanstead3254
@leighwanstead3254 Жыл бұрын
Which visual studio extension you demo to import use? Thanks in advance
@wiiznokes2237
@wiiznokes2237 Жыл бұрын
When calling sorted and unique, there two itération of the vec or there are combine in one iteration?
@frittex
@frittex Жыл бұрын
nice one
@scheimong
@scheimong Жыл бұрын
Itertools is great. Perhaps `rstest` next?
@adriankal
@adriankal Жыл бұрын
What happens if one date is corrupted? Whole line is dismissed or just the date is flagged as invalid?
@qm3ster
@qm3ster Жыл бұрын
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)
@praveenperera
@praveenperera Жыл бұрын
Premature optimization
@EgnachHelton
@EgnachHelton Жыл бұрын
Using a hashtable/btreeset would not be optimal for this. It's better to put them into a vector and sort the vector. Then you can deduplicate in one pass with O(1) space since all duplicates would be adjacent to each other after being sorted. In term of time complexity it's the same but using simpler data structures like vector is generally much faster than complexer ones like trees or hashtable. Calling .sorted().dedup().collect_vec() would exactly do that under the hood without extra allocations.
@qm3ster
@qm3ster Жыл бұрын
@@EgnachHelton I agree, as long as the data is realistic and not a degenerate case of all duplicates. Bravo!
@JohnPywtorak
@JohnPywtorak 5 ай бұрын
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.
@kwinzman
@kwinzman 9 ай бұрын
You should probably call sorted() before you call unique()
@PipeyardCentipede
@PipeyardCentipede Жыл бұрын
CITIES WERE NOT SUPPOSED TO BE IN THE SKIES‼️‼️
@Max-mx5yc
@Max-mx5yc Жыл бұрын
personally i disagree with "first class iterator support". we can talk about that label when geneators become a stable feature.
@loganhodgsn
@loganhodgsn Жыл бұрын
My preferred strategy is to avoid the turbofish syntax. I would have put the type explicitly: let log1: ApacheLogEntry = log1.filter_map(|l| l.ok()?.as_ref().try_into().ok());
@xydez
@xydez 11 ай бұрын
Why are you using try_into instead of try_from? Couldn't you just write `ApacheLogEntry::try_from(..).ok()`?
Strings in Rust FINALLY EXPLAINED!
21:40
Let's Get Rusty
Рет қаралды 74 М.
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 288 М.
ОСКАР ИСПОРТИЛ ДЖОНИ ЖИЗНЬ 😢 @lenta_com
01:01
World’s Deadliest Obstacle Course!
28:25
MrBeast
Рет қаралды 149 МЛН
She ruined my dominos! 😭 Cool train tool helps me #gadget
00:40
Go Gizmo!
Рет қаралды 63 МЛН
The standard library now has all you need for advanced routing in Go.
13:52
Understanding Rust Closures aka. Anonymous Functions 🦀 💻
30:22
Trevor Sullivan
Рет қаралды 10 М.
Rust Data Modelling Without Classes
11:25
No Boilerplate
Рет қаралды 164 М.
Rust: Associated types for Iterator implementations
14:25
Trusty Bits
Рет қаралды 4,1 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 905 М.
Rust is not a faster horse
11:37
No Boilerplate
Рет қаралды 319 М.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 261 М.
Rust Interior Mutability - Sneaking By The Borrow Checker
16:02
Code to the Moon
Рет қаралды 65 М.
Concurrency in Rust - Message Passing
9:26
Let's Get Rusty
Рет қаралды 38 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 151 М.
Игровой Комп с Авито за 4500р
1:00
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 1,7 МЛН
Lid hologram 3d
0:32
LEDG
Рет қаралды 10 МЛН
#miniphone
0:16
Miniphone
Рет қаралды 3,7 МЛН
Best mobile of all time💥🗿 [Troll Face]
0:24
Special SHNTY 2.0
Рет қаралды 1,7 МЛН
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,4 МЛН