Closures in Rust

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 มิ.ย. 2024
  • The ultimate Rust lang tutorial. Follow along as we go through the Rust lang book chapter by chapter.
    📝Get the FREE Rust Cheatsheet: letsgetrusty.com/cheatsheet
    The Rust book: doc.rust-lang.org/stable/book/​​
    0:00​ Intro
    0:29 What Are Closures?
    0:50 Example Program
    2:55 Refactoring With Functions
    4:07 Refactoring Using Closures
    6:31 Type Inference And Annotation
    8:06 Generic Parameters And Fn Traits
    15:33 Capturing the Environment with Closures
    19:12 Outro
    #letsgetrusty​​ #rust​lang​ #tutorial
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 95

  • @letsgetrusty
    @letsgetrusty  3 ปีที่แล้ว +13

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

  • @kid_kulafu_1727
    @kid_kulafu_1727 2 ปีที่แล้ว +14

    god damn this is a fucking gem. Im tired of reading and falling asleep. Thank my dude!

  • @ladyViviaen
    @ladyViviaen 3 ปีที่แล้ว +24

    finally learned closures, it seems to just be a function that captures its env and has extremely weird syntax

    • @noahhastings6145
      @noahhastings6145 2 ปีที่แล้ว +7

      *_extremely_*

    • @greyshopleskin2315
      @greyshopleskin2315 ปีที่แล้ว +4

      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)

  • @tino6986
    @tino6986 2 ปีที่แล้ว +11

    Excellent work! I'm closer to fully understanding closures, but another video on them with more examples would be great!

  • @LiiittleBigPlanet
    @LiiittleBigPlanet 3 ปีที่แล้ว +27

    Hey 👋🏻 great Video! I would love to see more about closures. Especially more complicated things with structs

  • @RoamingAdhocrat
    @RoamingAdhocrat ปีที่แล้ว +7

    did you choose the random number 7 with a fair dice roll?

  • @mariavarvaroi
    @mariavarvaroi ปีที่แล้ว +3

    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! ♥

  • @Marcus_613
    @Marcus_613 2 หลายเดือนก่อน

    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 !

  • @maksymiliank5135
    @maksymiliank5135 ปีที่แล้ว +4

    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

  • @karstenmeinders4844
    @karstenmeinders4844 ปีที่แล้ว +3

    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)

  • @thanhliemtran1375
    @thanhliemtran1375 2 ปีที่แล้ว +4

    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.

  • @SauvikRoy
    @SauvikRoy ปีที่แล้ว +1

    This was such a beautiful example!
    I'm liking rust!
    Thanks!

  • @Zombehmoviez
    @Zombehmoviez ปีที่แล้ว +1

    This explanation helped me understand closures a lot better after reading about it in the book. Nice video!

  • @samansamani4477
    @samansamani4477 2 ปีที่แล้ว +2

    man really enjoyed your explanation
    it almost make me want to learn rust...

  • @froggyana
    @froggyana 8 หลายเดือนก่อน +1

    You really explain very well. Thank you!

  • @marko-lazic
    @marko-lazic 3 ปีที่แล้ว +4

    Yes, more closures please.

  • @azephiar3863
    @azephiar3863 ปีที่แล้ว +1

    Your content is incredibly good, keep it up!

  • @kavehtehrani
    @kavehtehrani หลายเดือนก่อน

    Amazing explanation!

  • @sagnikbhattacharya1202
    @sagnikbhattacharya1202 3 ปีที่แล้ว +3

    Great video, very insightful!

  • @nsgirish
    @nsgirish ปีที่แล้ว +1

    Very well explained sir. Thanks

  • @dappdevelopment1927
    @dappdevelopment1927 ปีที่แล้ว +2

    ***cough*** I would also love to see an ***"Intermediate Rust"*** series! Please!

  • @Kor03d
    @Kor03d 4 หลายเดือนก่อน

    I unironically love how you pronounce "шtruct". I am going to annoy so many people copying this

  • @lukaszkonsek7940
    @lukaszkonsek7940 ปีที่แล้ว +2

    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

  • @bjugdbjk
    @bjugdbjk 2 ปีที่แล้ว +1

    More stuff on closures plsss. !!! By the way the way u teach is fantastic 👍

  • @UltimateMonkeyman
    @UltimateMonkeyman 2 ปีที่แล้ว +1

    Please do more video about Closures I would like to how to use them in many examples, Thank you for your video

  • @seenlenz
    @seenlenz ปีที่แล้ว +3

    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
      @maksymiliank5135 ปีที่แล้ว +2

      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.

  • @murugarajuperumalla5508
    @murugarajuperumalla5508 2 ปีที่แล้ว

    Yes please, continuation for closure again

  • @kevinwang6188
    @kevinwang6188 2 ปีที่แล้ว +2

    Great work and awesome series! I enjoyed every bit of these!

  • @tanakahirosi6453
    @tanakahirosi6453 ปีที่แล้ว +1

    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.

  • @bimaadi6194
    @bimaadi6194 3 ปีที่แล้ว +3

    How about tutorial on procedural macros in rust? btw nice video as always

  • @tiagomanczak
    @tiagomanczak 2 ปีที่แล้ว +1

    Please more closure videos.

  • @ME-dg5np
    @ME-dg5np 9 หลายเดือนก่อน

    Well done !🎉

  • @n0kodoko143
    @n0kodoko143 ปีที่แล้ว

    I finally understand closures

  • @saaddahmani1870
    @saaddahmani1870 2 ปีที่แล้ว

    Great...Thanks..

  • @andreasprues1980
    @andreasprues1980 3 ปีที่แล้ว +1

    Nice Video 😀

  • @nickandrievsky5705
    @nickandrievsky5705 2 ปีที่แล้ว +18

    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
      @demyk214 6 หลายเดือนก่อน

      No

    • @nickandrievsky5705
      @nickandrievsky5705 6 หลายเดือนก่อน +1

      @@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
      @demyk214 6 หลายเดือนก่อน

      @@nickandrievsky5705 no

    • @nickandrievsky5705
      @nickandrievsky5705 6 หลายเดือนก่อน

      @@demyk214 no what?

    • @demyk214
      @demyk214 6 หลายเดือนก่อน

      @@nickandrievsky5705 no

  • @RAHUDAS
    @RAHUDAS ปีที่แล้ว

    Yep I need another video

  • @codelearner4449
    @codelearner4449 ปีที่แล้ว +1

    One more video for closures would be perfection. Adding on to @JustinMcConnell you are actuallying making rust very approachable.

  • @daque1960
    @daque1960 2 ปีที่แล้ว +2

    Thanks much! This seemed more how to make a chacher than a closure. Still great👍👍

  • @reimner
    @reimner 10 หลายเดือนก่อน +1

    this is like lambda fuction in python, but has way more functionalities

  • @Gabonidaz
    @Gabonidaz ปีที่แล้ว

    If i get it right closures are like lambda functions in javascript or python

  • @hitesharora4883
    @hitesharora4883 6 หลายเดือนก่อน

    please add another video on closures

  • @activelow9297
    @activelow9297 15 วันที่ผ่านมา

    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.

  • @mario7501
    @mario7501 ปีที่แล้ว +1

    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.

  • @TheBlueye13
    @TheBlueye13 3 หลายเดือนก่อน

    Is this really the best way to cache function calls? This seems like a lot of boilerplate code that could be implemented in std.

  • @Megalcristo2
    @Megalcristo2 ปีที่แล้ว +1

    Closure are functions that capture some non-local value. Anonymous functions are functions with no name

  • @marcoantonio7648
    @marcoantonio7648 ปีที่แล้ว

    Does an anonymous function always implies into a closure?

  • @GolangDojo
    @GolangDojo 3 ปีที่แล้ว +1

    GIVE US THE COURSE!!!

  • @manishfoodtechs
    @manishfoodtechs 3 ปีที่แล้ว

    Please some of your videos in play list are private. Shall I ignore them?

    • @letsgetrusty
      @letsgetrusty  3 ปีที่แล้ว +1

      Yes, ignore them. Those are unpublished videos. I'll look into removing them from the playlist.

  • @larsthomasdenstad9082
    @larsthomasdenstad9082 ปีที่แล้ว +1

    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?

  • @JSWarcrimes
    @JSWarcrimes 6 หลายเดือนก่อน

    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().

  • @thebsv
    @thebsv 3 หลายเดือนก่อน

    Why do we need closures?

  • @jaysalunke4214
    @jaysalunke4214 2 ปีที่แล้ว

    Rust is interesting

  • @grvxs9230
    @grvxs9230 3 ปีที่แล้ว +3

    Can you upload more frequently? Like twice a week.

    • @letsgetrusty
      @letsgetrusty  3 ปีที่แล้ว +2

      Not something I have the bandwidth for right now but hopefully soon.

  • @michalbotor
    @michalbotor ปีที่แล้ว +1

    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.

  • @jonathanmoore5619
    @jonathanmoore5619 3 ปีที่แล้ว +5

    So essentially, lambda functions in c++... Rust style.

  • @yapayzeka
    @yapayzeka 2 ปีที่แล้ว +1

    instead of telling with words I think you should've cargo run.

  • @XionicalXionical
    @XionicalXionical 11 หลายเดือนก่อน +3

    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.

  • @ardhenismuhammadaflah7818
    @ardhenismuhammadaflah7818 3 ปีที่แล้ว +1

    first

  • @echoptic775
    @echoptic775 2 ปีที่แล้ว

    Deci be bodange

  • @AIstoriesHQ
    @AIstoriesHQ ปีที่แล้ว +3

    Overcomplicated example

  • @dmytrobortnichuk8501
    @dmytrobortnichuk8501 ปีที่แล้ว

    Вперше стикаюсь що в мові кложурс так сильно відрізняються від звичайних функцій

  • @snatvb
    @snatvb 11 หลายเดือนก่อน

    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

  • @pdform
    @pdform ปีที่แล้ว +1

    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...

  • @umut8244
    @umut8244 2 ปีที่แล้ว +3

    Examples are overkill...

  • @someoneelse5005
    @someoneelse5005 3 หลายเดือนก่อน

    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
    @gammyhorse 8 หลายเดือนก่อน

    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?