Crust of Rust: Iterators

  Рет қаралды 102,229

Jon Gjengset

Jon Gjengset

Күн бұрын

Пікірлер: 93
@howardmarles2576
@howardmarles2576 4 жыл бұрын
As a Rust beginner, I find your videos invaluable. Thank you Jon !!
@artgreg2296
@artgreg2296 4 жыл бұрын
i exactly think the same. its crazy this is free content
@dude2542
@dude2542 Ай бұрын
Update on your expertise?
@jeremycoleman3282
@jeremycoleman3282 4 жыл бұрын
I love that your inclination is to show the correct use first. So many programmers preface their guides with the wrong way first and it makes me rage
@theninjascientist689
@theninjascientist689 Жыл бұрын
it puts me on edge when I hear "so we could do it like this..." in a programming tutorial. I then have no idea if it's a valid alternative or just plain wrong until I watch the rest of the video
@jackmead7292
@jackmead7292 4 жыл бұрын
I am so, so, so tired of writing javascript and python. The most complicated thing I do all day is refactor crapping javascript formatting and try to convince people to follow better eslint rules, learn yet another annoying new npm package that probably has way to many dependencies, or have to constantly keep CTRL-F 'ing through a crappy python function that is 100 lines long. I am ready for Rust. Let's go!! Great video. Great explanation. Thank you so much 🙏
@vorname1485
@vorname1485 4 жыл бұрын
I use typescript in fully strict mode, when I have to work with javascript.
@vorname1485
@vorname1485 4 жыл бұрын
10:00 into_iter takes the ownership of the value, which is the meaning of "into", the ownership is transfered "into" the iterator. value.into_iter() makes sense this way :)
@vorname1485
@vorname1485 4 жыл бұрын
@@YuruCampSupermacy correct :)
@jonhoo
@jonhoo 4 жыл бұрын
Yup, that's all correct. Keep in mind as I show later on though that `iter` is an inherent method, while `into_iter` is a trait method from the `IntoIterator` trait. Which means that other types and methods can be generic over it. This is why you'll still want to implement `IntoIterator` for, say, `&Vec` (which would effectively do the same as `Vec::iter`)
4 жыл бұрын
> into_iter takes the ownership of the value, which is the meaning of "into", the ownership is transfered "into" the iterator True, though I want to point out something that had me confused for a long time, in case it helps somebody else: `into_iter` taking ownership initially led me to think that it always consumes the underlying collection. That's not necessarily the case though, because that "value" can be a reference, in which case `into_iter` takes ownership of a reference (a.k.a. precious little). E.g. there's an impl of `IntoIterator` for `&Vec`: doc.rust-lang.org/src/alloc/vec.rs.html#1971-1978 (which basically just defers to `Vec::iter`, as Jon says). Here's a playground with a bit of code that helped me clear up that confusion: play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4a6c2e0230d98237e7ad86d239db4655 (I'm only part way through watching the video -- which is great as always, thanks! -- so apologies if this is addressed somewhere in there :) Still, hopefully someone might find it useful to have this information in the comments as well.)
@luiswirth
@luiswirth 4 жыл бұрын
@ Thanks David, now I get it!
@starczarar
@starczarar 8 ай бұрын
I agree, the author didn't know this. "It’s easier to type" was his guess.
@fredrikjonsen6222
@fredrikjonsen6222 4 жыл бұрын
Really enjoying these Crust of Rust videos. The more lengthy videos are interesting, but I often feel lost halfway through. The shorter ones feel more relevant when you're still learning the basics but have gotten through the book.
@connormatza5873
@connormatza5873 Ай бұрын
this is the best rust content produced, ever. Most rust devs or wannabes have programmed a computer before so will quickly get tired of "from zero" tutorials
@jonas-mm7em
@jonas-mm7em 3 жыл бұрын
Thank you Jon for all these learning materials. This is definitely helpful. I'm going through the whole crust of rust series and then I'll go through your book as well. I already book the pre-release. 💪😁
@vroomerlifts
@vroomerlifts 2 жыл бұрын
I just learnt iterator design pattern and now this, was super relatable.
@Mahmudul_Hasan92
@Mahmudul_Hasan92 Жыл бұрын
I am learning so much from you. Working with rust code from your streams and going from (kind of getting it) to (okay I actually GET it) feels oddly satisfying, and I have never felt like this while learning other languages over the years.
@KiffinGish
@KiffinGish 4 жыл бұрын
Perfect presentation, I look forward to following your series as more presentations are made!
@abdulazizalabodi
@abdulazizalabodi 4 жыл бұрын
Thank you for the video! I have used iterators in other languages such as java and I am fairly familiar with the concept. But I still found your explanation useful.
@wvanmourik1
@wvanmourik1 4 жыл бұрын
Thank you so much for these series. Really happy to see these type of videos about advanced Rust topics.
@Tantandev
@Tantandev 4 жыл бұрын
Thank you for these videos! Very helpfull!
@brod515
@brod515 11 ай бұрын
I'm leaning rust but... but one things I've started to notice (especially in this video ) is how crucial testing is and how rust makes it easy to encourage you to write them...
@unspecifiedsender9035
@unspecifiedsender9035 2 жыл бұрын
This is the next rust video I've watched
@damickillah
@damickillah Жыл бұрын
76K views should equal 76K likes. The value in this man's content is incomprehensible, in my opinion(s).
@RajaKhan-dc4vs
@RajaKhan-dc4vs 4 ай бұрын
true, I recently have started to explore his playlists. he is next level programmer. He is bringing so much value , low level understanding of iterator, macros and other stuffs which we can use to rewrite the industry level framework stacks from scratch in rust itself. although i am keeping the hype aside. but the core rust is immense valuable and future .
@kurjak9595
@kurjak9595 4 жыл бұрын
Hey, can you put together a stream for the topic Smart pointers? I personally read the book about 2-3 times and still don't get the Cell and RefCell pointers.
@AryungChen
@AryungChen Жыл бұрын
It's so great about how to think and try to implement by self to learn. By the way, is there any vim/nvim config in the video? It is good to have such lint related info or error show up. Thanks
@ramazangCoder
@ramazangCoder 4 күн бұрын
omg! That is crazy man! Jeg elsker det
@veetaha
@veetaha 4 жыл бұрын
It would be cool to upload the streams as full-fledged YT streams such that the chat is also recorded and we can see it after the fact, but I am not sure if it is possible to cut the intro bootstrap part...
@jonhoo
@jonhoo 4 жыл бұрын
The live stream version with the comments is already linked at the bottom of the description :)
@TON-vz3pe
@TON-vz3pe 7 ай бұрын
How does flatten even call thr next() method in the iterator? All I see in code is a new() and getting a Flatten instance
@Yotanido
@Yotanido 3 жыл бұрын
I don't know if it's a conscious choice or you don't know about it, but coc offers rename refactoring with (coc-rename) I noticed you do a lot of manual renaming, so I thought I'd point it out. I've personally got it mapped to F3
@haphamdev
@haphamdev 3 жыл бұрын
Thank you very much for this nice series. Could you read the questions from comments slowly so that we can hear them?
@uoweme5grand
@uoweme5grand 2 жыл бұрын
Holy crap there is a person behind you. I did not see that until the end.
@NoNameAtAll2
@NoNameAtAll2 3 жыл бұрын
9:58 iter and into_iter are 2 different things, aren't they? Iter is "read-only access" and into_iter is "consuming access" In what way do they do same thing?
@rurunosep
@rurunosep Жыл бұрын
I think the idea is that a.iter() is effectively just an alias for &a.into_iter(), and a.iter_mut() is also effectively just an alias for &mut a.into_iter(). into_iter() will give "read-only access", "mutating access", or "consuming access" depending on the context. You could also think of it as consuming the references when called with a reference. iter() and iter_mut() are redundant but convenient.
@sayakchatterjee4454
@sayakchatterjee4454 4 жыл бұрын
I just realized that even after trying to learn Rust for over a year, I still don't understand so much of the basics. 😭
@jonathanguillotte-blouin4327
@jonathanguillotte-blouin4327 4 жыл бұрын
Have you tried to write something in Rust? If not, I would recommend to do so! As many things in life, you can only get better by practicing. :)
@user-ov5nd1fb7s
@user-ov5nd1fb7s 4 жыл бұрын
Great videos, thanks.
@danieledepetris4582
@danieledepetris4582 4 жыл бұрын
Hi Jon thanks for your evangelism on Rust, I have a tip for your videos on KZbin you should if possible enable English subtitles
@jonhoo
@jonhoo 4 жыл бұрын
They are already enabled, but KZbin unfortunately isn't great about adding automatic captions for longer videos. Community captions are enabled though if someone feels a calling!
@mavichovizana5460
@mavichovizana5460 3 жыл бұрын
I think the reason that `O::item::item` could not compile is because O::item could be associated constant. This is same with C++, where you need to use `typename` to tell the compiler that `item` is a type not a value.
@darthmoomoo
@darthmoomoo 3 жыл бұрын
With the extension trait, will deep() work without "flatten(flatten" ? Your unit test only goes to two levels.
@metamemelord
@metamemelord 4 жыл бұрын
That's a pretty nice vim skin, which one is it?
@jonhoo
@jonhoo 4 жыл бұрын
It's called gruvbox-dark-hard :)
@DavideVegliante
@DavideVegliante 3 жыл бұрын
Thank you! This is great
@_sandeepnambiar
@_sandeepnambiar 4 жыл бұрын
What is your desktop setup? It looks quite nice!
@jonhoo
@jonhoo 4 жыл бұрын
I actually did a full video on that over at kzbin.info/www/bejne/r5SwmoB6fdSjhMU :)
@Singlton
@Singlton 4 ай бұрын
what about implementing the iter() trait for a LinkedList , its so hard !!!
@DerH0ns
@DerH0ns 4 жыл бұрын
Isn't your approach to implementing DoubleEndedIterator wasting a lot of memory in the case where we don't actually use DoubleEndedIterator?
@jonhoo
@jonhoo 4 жыл бұрын
I touch on this towards the end of the video, and why it currently has to be there. You're right that it's wasting a bit of memory, but keep in mind that it's just storing an extra iterator, which is itself unlikely to be very large (a hash map iterator for example is mostly just a couple of pointers). In practice, it's also unlikely to matter much, since it is stack allocated, and iterators are usually short-lived. It is unlikely that you have, say, hundreds of thousands of `Flatten` iterators active at the same time.
@xrafter
@xrafter 3 жыл бұрын
Why does &[T] slices implement iterator but normal arrays don't ? Edit: from rust 1.54 the array primitive implement the iterator trait
@jonhoo
@jonhoo 3 жыл бұрын
Take a look at blog.rust-lang.org/2021/05/11/edition-2021.html#intoiterator-for-arrays, which gives a lot of context for that particular nuance.
@ksleo2328
@ksleo2328 2 жыл бұрын
This video has no subtitles, could anyone explain why?
@michaelflynn6952
@michaelflynn6952 2 жыл бұрын
I feel like every topic you introduced you just said "but this isnt what we're here to talk about" or "but that isn't important right now" and it left me wondering what IS being demonstrated
@niklashalonen9296
@niklashalonen9296 4 жыл бұрын
At 42:00 you didn't remove `self.inner = None` from line 43. The tests pass without the line just fine. What is the reason for leaving it in?
@jonhoo
@jonhoo 4 жыл бұрын
It works fine because the inner iterator also keeps track of the fact that it's been exhausted. So next time around, we'll enter the outer `if let`, but the call to `next()` will yield `None`, and so we'll fall down to the code below again. But that means we're now relying on it being okay to call `next()` on the inner iterator after it has returned None, which is not the case for all iterators!
@rzlink001
@rzlink001 18 күн бұрын
Looks like someone is playing video game in the couch.
@johnbotris8187
@johnbotris8187 4 жыл бұрын
I'm not sure if this gets answered later on or if I missed something, but regarding the Flatten implementation: is there a particular reason why O is bounded to Iterator, but O::Item is bounded to into Iterator? why not the other way round or have them both be the same?
@jonhoo
@jonhoo 4 жыл бұрын
I don't know if this became clearer throughout the stream, but the answer is that the outer iterator has to be Iterator because we store the outer iterator itself directly in the struct (if it was IntoIterator, and we stored a O::IntoIter, I _think_ the compiler would complain). We also don't _need_ the Into part of IntoIterator in Flatten, so why make the bound larger than it needs to be? For the inner iterator however, we _are_ going to turn each element of the outer iterator _into_ an Iterator, so we need it to implement IntoIterator. If it just implemented Iterator, then we'd need to mutate the iterator we're walking over directly!
@user-ov5nd1fb7s
@user-ov5nd1fb7s 4 жыл бұрын
On a side note, I think you hold the Guinness world record for saying iterator a gazillion times in an hour.
@meuko
@meuko 4 жыл бұрын
I wonder if this being a video about... iterators had any part in that.
@user-ov5nd1fb7s
@user-ov5nd1fb7s 4 жыл бұрын
@@meuko it was a joke man....jeez
@meuko
@meuko 4 жыл бұрын
@@user-ov5nd1fb7s Hey dude I was also joking, sorry
@jeffg4686
@jeffg4686 3 жыл бұрын
I find it interesting that they choose to use & when denoting a parameter must be borrowed (ref), but for patterns they use the 'ref' keyword. That tripped me up when I was first learning rust. Any thoughts on why they chose to spell it out in patterns (maybe just to indicate it's a pattern) ?
@jonhoo
@jonhoo 3 жыл бұрын
The two are actually not the same. They're more like opposites. A &foo in a pattern is "match foo as what's behind the reference", whereas ref foo is "match foo as a reference".
@jeffg4686
@jeffg4686 3 жыл бұрын
@@jonhoo - ah gotcha, definitely big difference there. much thanks
@kostareg
@kostareg 3 жыл бұрын
39:00 couldn't you just call inner_iter.into_iter().next() instead of doing all of that work with trait bounds?
@velimirchakhnovski2380
@velimirchakhnovski2380 7 ай бұрын
I'm still about halfway through the video (44:50), and it seems we finished talking about flatten, but can't it be improved? I don't see why it would need an Option for the inner iterator. We could have new like this: ``` let inner = iter.next(); Flatten { outer: iter, inner } ``` and next like this: ``` loop { if let Some(elem) = self.inner.next() return Some(elem); self.inner = self.outer.next()?; } ``` This seems more elegant than the implementation of next that's shown in the video.
@velimirchakhnovski2380
@velimirchakhnovski2380 7 ай бұрын
I can see that this wouldn't work for DoubleEndedIterator. We would need to call next_back in new, and so our Flatten wouldn't accept iterators that don't implement DoubleEndedIterator. Although it seems to me that Flatten and DoubleEndedFlatten should have been two types anyways, given that they don't need the same fields... Maybe in practice it's better to have one type for both use cases, but I'm not experienced enough to judge when that might be the case.
@georgespringbach2062
@georgespringbach2062 4 жыл бұрын
Rust is cool! U are cool 😎 too Jon!
@jeffg4686
@jeffg4686 3 жыл бұрын
Can anyone say if an Associated Type basically just a typedef nested into a type, or is there more to it than that? Sounded like a new concept or something when first learning rust, and some compare to generics, but it's hard-coded to some type (which may or may not be a generic type). But a direct comparison to generics doesn't really seem to make sense to me at all - even if the type being defined leverages generics. Associated type is just a nested typedef, or I am actually way off here?
@jonhoo
@jonhoo 3 жыл бұрын
Hi Jeff! An associated type is sort of like a nested typedef, but it is nested not per type, but per implementation of a trait. For example, if some type Foo implements the trait Bar twice for two different Ts, it can use different types for an associated type of Bar for those two impls. Associated types can also have trait bounds, and can be referenced elsewhere in the trait. For example, the Deref trait from the standard library has an associated type Target, and a method fn deref(&self) -> &Self::Target.
@jeffg4686
@jeffg4686 3 жыл бұрын
@@jonhoo - great answer, thanks
@adicide9070
@adicide9070 Жыл бұрын
peeps, what is this font used here in vim? anyone know?
@well.8395
@well.8395 Жыл бұрын
I think Monolisa.
@adicide9070
@adicide9070 Жыл бұрын
@@well.8395 noto sans mono, actually. said in the q&a for july 23 today.
@shironbenyamini2798
@shironbenyamini2798 3 жыл бұрын
Not sure I understand the purpose of associated types and why not use generics. If all they're for is as you said, to make life easier for the compiler and maybe shave off a second of compile time, why not just use generics and be done with it? surely there's more to it then that? I can't imagine the core team introduced this language "construct" just for that? Does it provide additional benefits?
@jonhoo
@jonhoo 3 жыл бұрын
As I alluded to in the video, they enable you to constrain implementation of the trait to only a single instance per type, which can often times be necessary to get type inference to work. Associated types also tend to be more ergonomic to use, precisely because they're more constrained. Take the Future trait for example - you don't need to specify the Output type every time you take a F: Future, because the Output type is associated, and thus dictated by F.
@trolledwoods377
@trolledwoods377 4 жыл бұрын
Jo(h)n gang!!!
@haI9k
@haI9k 4 жыл бұрын
And now something completely different ;) What is the name of this zsh option/extension which shows suggestion based on history? e.g. 2:38
@jonhoo
@jonhoo 4 жыл бұрын
I use the fish shell, not zsh, which does this by default :)
@Dygear
@Dygear 2 жыл бұрын
I have the feeling that your local IP address 10.0.1.27 is a play on 127.0.0.1.
@SeniorMarsTries
@SeniorMarsTries 3 жыл бұрын
What is that drawing program?
@jonhoo
@jonhoo 3 жыл бұрын
It's called MyPaint :)
@syphiliticpangloss
@syphiliticpangloss 2 жыл бұрын
Over some explaining concepts. Too little on syntax craziness.
@mcpecommander5327
@mcpecommander5327 11 ай бұрын
I don’t think your explanation of .iter vs .into_iter is correct. .into_iter is a trait method that consumes the value, and generally gives an iterator that gives the values in the collection by-value. .iter is a naming convention for a method that takes the collection by-reference and returns an iterator that iterates also by-reference. Also, slice ([T]) does not implement IntoIterator, while &[T] does (which is also common for types that can be .iter-ed to have references to them implement IntoIterator)
@usercommon1
@usercommon1 7 ай бұрын
I fell asleep while watching this video😭
@well.8395
@well.8395 Жыл бұрын
I like your videos Jon, but can you avoid doing Tsskk sound every minute or so. It's very irritating to me someone suffering from misophonia. Thank you very much for the content 👍
Crust of Rust: Smart Pointers and Interior Mutability
2:03:04
Jon Gjengset
Рет қаралды 128 М.
Crust of Rust: Declarative Macros
1:36:11
Jon Gjengset
Рет қаралды 67 М.
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 65 МЛН
What type of pedestrian are you?😄 #tiktok #elsarca
00:28
Elsa Arca
Рет қаралды 27 МЛН
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 84 МЛН
Rust: Iterators
20:07
The Dev Method
Рет қаралды 15 М.
Crust of Rust: Functions, Closures, and Their Traits
1:06:40
Jon Gjengset
Рет қаралды 91 М.
Rust's iterators are more interesting than they look
22:46
timClicks
Рет қаралды 9 М.
Iterators in Rust
13:25
Let's Get Rusty
Рет қаралды 53 М.
Why I Chose Rust Over Zig
33:18
ThePrimeTime
Рет қаралды 208 М.
Crust of Rust: Channels
1:43:12
Jon Gjengset
Рет қаралды 82 М.
Crust of Rust: Lifetime Annotations
1:33:23
Jon Gjengset
Рет қаралды 221 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 152 М.
Solving distributed systems challenges in Rust
3:15:52
Jon Gjengset
Рет қаралды 275 М.
Crust of Rust: Send, Sync, and their implementors
1:07:04
Jon Gjengset
Рет қаралды 45 М.
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 65 МЛН