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 Жыл бұрын
📝Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@ShiyalaKohnythis guy sorts!
@ShiyalaKohny11 ай бұрын
@@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)
@aperson405111 ай бұрын
@@ShiyalaKohny it's a meme, giving you praise for your sorting knowledge. Nothing serious!
@kwinzman8 ай бұрын
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 Жыл бұрын
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 Жыл бұрын
Dude this video is really good. You mentioned a lot of stuff that I get hung up on when writing rust. Thanks!
@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 Жыл бұрын
Doesn't chain() from std already provide the same functionality marge() gives?
@billhurt3644 Жыл бұрын
Rubyists like me love that Rust can iterate over collections this easily.
@fabienpenso Жыл бұрын
Agreed. All my rubyist friends love it as well.
@gpcuretonАй бұрын
Thanks, this video is a banger.
@wuerges Жыл бұрын
You could use the identity function from std::convert to annotate the type
@flogginga_dead_horse4022 Жыл бұрын
super useful!
@GlobalYoung7 Жыл бұрын
thank you ❤
@paulobitfranca6 ай бұрын
very cool !!!!
@TheRedbeardster Жыл бұрын
Great!
@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 Жыл бұрын
No one: literally no one: me who's portuguese looking at the thumbnail: EU SOU O BOB CONSTRUTOR, È O BOB TRABALHADOR
@leftyhero147 Жыл бұрын
kkkkkkk
@leighwanstead3254 Жыл бұрын
Which visual studio extension you demo to import use? Thanks in advance
@wiiznokes2237 Жыл бұрын
When calling sorted and unique, there two itération of the vec or there are combine in one iteration?
@frittex Жыл бұрын
nice one
@scheimong Жыл бұрын
Itertools is great. Perhaps `rstest` next?
@adriankal Жыл бұрын
What happens if one date is corrupted? Whole line is dismissed or just the date is flagged as invalid?
@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 Жыл бұрын
Premature optimization
@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 Жыл бұрын
@@EgnachHelton I agree, as long as the data is realistic and not a degenerate case of all duplicates. Bravo!
@JohnPywtorak5 ай бұрын
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.
@kwinzman9 ай бұрын
You should probably call sorted() before you call unique()
@PipeyardCentipede Жыл бұрын
CITIES WERE NOT SUPPOSED TO BE IN THE SKIES‼️‼️
@Max-mx5yc Жыл бұрын
personally i disagree with "first class iterator support". we can talk about that label when geneators become a stable feature.
@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());
@xydez11 ай бұрын
Why are you using try_into instead of try_from? Couldn't you just write `ApacheLogEntry::try_from(..).ok()`?