📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@kid_kulafu_17273 жыл бұрын
god damn this is a fucking gem. Im tired of reading and falling asleep. Thank my dude!
@RoamingAdhocrat Жыл бұрын
did you choose the random number 7 with a fair dice roll?
@ladyViviaen3 жыл бұрын
finally learned closures, it seems to just be a function that captures its env and has extremely weird syntax
@noahhastings61452 жыл бұрын
*_extremely_*
@greyshopleskin2315 Жыл бұрын
I think a good way to think about closures, if you are familiar with oop but not fp, is that a closure is a class with only 1 method, and a constructor that receives all vars in current scope. So this: def a(): x = 1 def closure(): return x * 2 return closure Is similar to: class Closure: def __init__(self, x): self.x = x def call(self): return self.x def a(): x = 1 return Closure(x)
@karstenmeinders48442 жыл бұрын
At about 15:00: IMHO, this technique is called "memoization" = storing results of expensive function calls in case of subsequent calls? Great to see that this can be quite easily be implemented in Rust (even if Haskell and the like have it built-in)
@Marcus_6139 ай бұрын
Thank you, as a Rust beginner, this one really helped me understanding how closure works. You've build an awesome channel for anyone learning Rust !
@tino69863 жыл бұрын
Excellent work! I'm closer to fully understanding closures, but another video on them with more examples would be great!
@nickandrievsky57052 жыл бұрын
Thank you for interesting vid. Sorry, but lazy evaluation pattern has nothing to do with closures. Your example is over complicated. It could be way more interesting if you show different ownership examples and pitfalls of closures.
@demyk214 Жыл бұрын
No
@nickandrievsky5705 Жыл бұрын
@@demyk214 What do you mean? Closures: Anonymous Functions that Capture Their Environment. As simple as that. Caching and other stuff works perfectly well with normal functions.
@demyk214 Жыл бұрын
@@nickandrievsky5705 no
@nickandrievsky5705 Жыл бұрын
@@demyk214 no what?
@demyk214 Жыл бұрын
@@nickandrievsky5705 no
@UdiTalias2 ай бұрын
Your explanation that FnOnce, FnMut, and Fn are the same as calling to a regular function did the trick for me. FnOnce is a closure trait for - fn foo(val: i32) { ... } FnMut is a closure trait for - fn foo(val: &mut i32) { ... } Fn is a closure trait for - fn foo(val: &i32) { ... }
@jmsdnns3 ай бұрын
Love your videos! This one was fantastic. I've used closures in other languages a lot so this was great for a way to see how Rust does it.
@seenlenz2 жыл бұрын
Hi there, i have a question. at 10:59 on line 40, v is declared and set equal to (self.calculation)(arg) where(self.calculation) is the closure and (arg) is the parameter. I understand what's happening but I'm having trouble with the syntax. I understand why (arg) is in parenthesis since this is the syntax for passing an argument to a closure, but why is self.calculation in parenthesis?
@maksymiliank5135 Жыл бұрын
If you don't use the parenthesis, the compiler is going to treat this expression as a method call. Since your type doesn't implement that method, it's going to result in an error. You need to wrap closures which are stored in a field of a struct in parenthesis to avoid ambiguity. EDIT: The reason why this is happening is that when you call a method (for example: object.method()), the object is going to be passed as the first argument called self. This is a syntactic sugar for Object::method(object), where you pass the object explicitly. When it comes to closures, they don't necessarily have to receive 'self' as an argument.
@LiiittleBigPlanet3 жыл бұрын
Hey 👋🏻 great Video! I would love to see more about closures. Especially more complicated things with structs
@letsgetrusty3 жыл бұрын
Noted!
@maksymiliank5135 Жыл бұрын
I thought that the pipes were a capture block, and the full closure definition was |captured_values|(arguments) -> return_type { code }. Thanks for the clarification
@shubhrantyadav15382 ай бұрын
Yo bro... This playlist is just too awesome. Thanks a lotttt
@activelow92977 ай бұрын
Really, all this rigamarole can be implemented in C or C++ by simply declaring your generator function with a static result member... and then tag it with a bool if it's been generated or not. Then you can call the generator to your heart's content and it will either generate the result (the first time) or return that result (every subsequent call).......... I'm not a Rust programmer, I am still learning, but I have watched 10-20 videos so far and I have yet to find a feature of Rust that you can't do in C++ if you choose to use the specific feature as it is implemented in C++. I am sure I will find something eventually that will prove this maxim wrong, but..... not yet.
@Kor03d10 ай бұрын
I unironically love how you pronounce "шtruct". I am going to annoy so many people copying this
@thanhliemtran13753 жыл бұрын
Thank you Bogdan, the videos that accompanies the book's examples are really usefull. I am new with Rust and your videos make the langage very approachable for a beginner like me.
@letsgetrusty3 жыл бұрын
You're very welcome!
@samansamani44772 жыл бұрын
man really enjoyed your explanation it almost make me want to learn rust...
@mariavarvaroi2 жыл бұрын
Great work, Bogdan! The videos are an awesome complement to the Rust book- I am watching your videos after reading the chapters, to gain extra understanding. Would love to see more videos on closures or any other subject. Your work is excellent, keep it up! ♥
@Zombehmoviez2 жыл бұрын
This explanation helped me understand closures a lot better after reading about it in the book. Nice video!
@SauvikRoy Жыл бұрын
This was such a beautiful example! I'm liking rust! Thanks!
@dappdevelopment19272 жыл бұрын
***cough*** I would also love to see an ***"Intermediate Rust"*** series! Please!
@froggyana Жыл бұрын
You really explain very well. Thank you!
@marko-lazic3 жыл бұрын
Yes, more closures please.
@nsgirish Жыл бұрын
Very well explained sir. Thanks
@Megalcristo22 жыл бұрын
Closure are functions that capture some non-local value. Anonymous functions are functions with no name
@lukaszkonsek7940 Жыл бұрын
I'm new in rust, but I'm surprised that there is no example with closures combined with a map. Seems a compelling technique for me
@sagnikbhattacharya12023 жыл бұрын
Great video, very insightful!
@azephiar38632 жыл бұрын
Your content is incredibly good, keep it up!
@bjugdbjk3 жыл бұрын
More stuff on closures plsss. !!! By the way the way u teach is fantastic 👍
@UltimateMonkeyman3 жыл бұрын
Please do more video about Closures I would like to how to use them in many examples, Thank you for your video
@kavehtehrani8 ай бұрын
Amazing explanation!
@reimner Жыл бұрын
this is like lambda fuction in python, but has way more functionalities
@teqnify635 ай бұрын
Why would closures be necessary with this struct? Not all the way through this video yet but it’s clear to me that you could simply use a normal Fn. This doesn’t seem like a good example until the end here when we talk about scope.
@n0kodoko143 Жыл бұрын
I finally understand closures
@daque19603 жыл бұрын
Thanks much! This seemed more how to make a chacher than a closure. Still great👍👍
@ME-dg5np Жыл бұрын
Well done !🎉
@tanakahirosi64532 жыл бұрын
Great content! Still find it hard to actually code in rust tho, I tried implementing some easy algorithms and it was quite a pain lol.
@murugarajuperumalla55083 жыл бұрын
Yes please, continuation for closure again
@bimaadi61943 жыл бұрын
How about tutorial on procedural macros in rust? btw nice video as always
@letsgetrusty3 жыл бұрын
On the todo list!
@marcoantonio7648 Жыл бұрын
Does an anonymous function always implies into a closure?
@codelearner44492 жыл бұрын
One more video for closures would be perfection. Adding on to @JustinMcConnell you are actuallying making rust very approachable.
@tiagomanczak2 жыл бұрын
Please more closure videos.
@TheBlueye1310 ай бұрын
Is this really the best way to cache function calls? This seems like a lot of boilerplate code that could be implemented in std.
@larsthomasdenstad9082 Жыл бұрын
Why is it called a closure when it's called a lambda in other languages and a closure is actually what a local function can pick up and address?
@kevinwang61883 жыл бұрын
Great work and awesome series! I enjoyed every bit of these!
@XionicalXionical Жыл бұрын
idk if its just me being tired but none of this made any sense. I dont know what it is, what it does, or what problem it solves.
@mario7501 Жыл бұрын
I really like rust so far. I'm a bit disappointed though that there is no equivalent to generic lambdas/closures in c++. In c++ you can use a generic closure on different data types and it just overloads the ()operator.
@Gabonidaz Жыл бұрын
If i get it right closures are like lambda functions in javascript or python
@RAHUDAS2 жыл бұрын
Yep I need another video
@ZephyTheseus3 ай бұрын
thank you
@hitesharora4883 Жыл бұрын
please add another video on closures
@JSWarcrimes Жыл бұрын
Looks so easy, when Bogdan is doing it... But god forbid trying to modify the example - changing value type to Option led me to some atrocious code with tons of .clone() and .take().
@thebsv9 ай бұрын
Why do we need closures?
@michalbotor2 жыл бұрын
doesn't rust support higher order functions? this all feels more complicated than it needs to be. if rust did support higher order functions we could pass the reference to our expensive function as a parameter to another function and call it only if it needs to be run.
@GolangDojo3 жыл бұрын
GIVE US THE COURSE!!!
@manishfoodtechs3 жыл бұрын
Please some of your videos in play list are private. Shall I ignore them?
@letsgetrusty3 жыл бұрын
Yes, ignore them. Those are unpublished videos. I'll look into removing them from the playlist.
@saaddahmani18703 жыл бұрын
Great...Thanks..
@agnesakne4409Ай бұрын
What's the value of your videos if the content is exactly that from the book?
@rexsybimatrimawahyu3292Ай бұрын
Tbh thats a fair question, maybe this vid is for those who are lazy reading the Book
@grvxs92303 жыл бұрын
Can you upload more frequently? Like twice a week.
@letsgetrusty3 жыл бұрын
Not something I have the bandwidth for right now but hopefully soon.
@AIstoriesHQ2 жыл бұрын
Overcomplicated example
@pdform Жыл бұрын
The explanation about closures is good, but the example the book uses (in the first part) is horrible. This example doesn't require closures at all, you could just use a regular function or a method without any disadvantage in terms of code simplicity. It's more an illustration of the memoization technique than of a typical use of closures...
@yapayzeka2 жыл бұрын
instead of telling with words I think you should've cargo run.
@jaysalunke42142 жыл бұрын
Rust is interesting
@jonathanmoore56193 жыл бұрын
So essentially, lambda functions in c++... Rust style.
@letsgetrusty3 жыл бұрын
Yup, same idea.
@snatvb Жыл бұрын
it's not closure, this is lambda functions, closure - it's when your function catch data from outside scope let x = 1; let foo = || x * 2; // the lambda closures x let bar = |a: u32, b: u32| a * b; // this is not closure
@tech34254 ай бұрын
yeye
@dmytrobortnichuk85012 жыл бұрын
Вперше стикаюсь що в мові кложурс так сильно відрізняються від звичайних функцій
@umut82443 жыл бұрын
Examples are overkill...
@echoptic7753 жыл бұрын
Deci be bodange
@ardhenismuhammadaflah78183 жыл бұрын
first
@someoneelse500510 ай бұрын
This video is actually pretty bad, the whole idea of what a closure is or does can be summarized in 30 seconds where you explain how it captures an outside variable, yet the video is 19 minutes, it tries to explain memoization, what an expensive call is etc, none of which are reasons to use a closure.
@gammyhorse Жыл бұрын
lol you think this is inexpensive because you printing only once? Dude, you're calling the constructor everytime, so what's the point? Have you benchmarked this method?