Understanding Rust Closures aka. Anonymous Functions 🦀 💻

  Рет қаралды 15,806

Trevor Sullivan

Trevor Sullivan

Күн бұрын

Пікірлер: 61
@TrevorSullivan
@TrevorSullivan Жыл бұрын
Check out the full Rust programming playlist! 🦀 kzbin.info/aero/PLDbRgZ0OOEpUkWDGqp91ODn0dk7LPBAUL
@adamszalkowski8226
@adamszalkowski8226 7 ай бұрын
Would have loved to see in this video concepts like `Fn`, `FnOnce`, when to use `move`, and function items vs pointers vs fn traits.
@kevinkkirimii
@kevinkkirimii 10 ай бұрын
The quality of your camera and the mic is sick and your tutorials are brilliant. Well done.
@TrevorSullivan
@TrevorSullivan 10 ай бұрын
Wow, thank you!! I'm glad you are learning from my channel!
@Takatou__Yogiri
@Takatou__Yogiri 3 ай бұрын
for real. i don't know why i didn't find his channel before.
@simonkalu
@simonkalu 8 ай бұрын
The best Rust closure tutorial on KZbin. Well done, Trevor!
@TrevorSullivan
@TrevorSullivan 8 ай бұрын
Thank you Simon! I hope you're enjoying the Rust language! 🦀
@lcssbr
@lcssbr Ай бұрын
I've seen many rust videos before and I'd never seen your channel being mentioned, which is surprising, given the quality of your content. I'll definitely watch a lot of other videos about rust in your channel. Thanks for the content!
@TrevorSullivan
@TrevorSullivan Ай бұрын
@@lcssbr thank you so much. That means a lot to me. I'm glad that my format is helpful for you! Keep learning Rust! 🦀
@mohsan5491
@mohsan5491 7 ай бұрын
// In VSCode, type inference for a closure looks like this: let print_number: impl Fn(u16) = |n: u16| println!("incoming: {0}", n); // However, in RustRover, the inferred type for the same closure is presented as: let print_number: fn(u16) = |n: u16| println!("incoming: {0}", n); Interesting how different IDEs handle Rust's type inference for closures. VSCode uses impl Fn(u16) to indicate a flexible closure that might capture the environment, while RustRover opts for fn(u16), suggesting a simpler, non-capturing function pointer. It's a neat insight into the nuances of Rust's type system and how tooling can vary in presenting it.
@mohsan5491
@mohsan5491 7 ай бұрын
when I tried to expicitly type "let print_number : impl Fn(u16)=" analyser did not complained but compiler threw this error error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> src/closures.rs:2:24 | 2 | let print_number : impl Fn(u16) = |n: u16| println!("incoming: {0}", n); | ^^^^^^^^^^^^ | = note: `impl Trait` is only allowed in arguments and return types of functions and methods
@TrevorSullivan
@TrevorSullivan 7 ай бұрын
That's interesting! Thanks for sharing.
@mikemardis
@mikemardis 7 ай бұрын
Like the way you cover the material, thanks. Disappointed not to find a video on the second half of the chapter in the Rust book, though. Are you planning on doing iterators?
@TrevorSullivan
@TrevorSullivan 7 ай бұрын
Thanks for the comment! I have a video on Rust iterators. Were you looking for something else?
@mohsan5491
@mohsan5491 7 ай бұрын
Hey Trevor, Thanks a ton for this Rust course playlist! Your effort truly shows in the high quality content. It's made learning Rust much clearer and enjoyable. Really appreciate your work.
@TrevorSullivan
@TrevorSullivan 7 ай бұрын
Thank you! I appreciate your kind comment and I'm very happy that this has helped you. That's all I hope to accomplish, making other people's lives better, by sharing knowledge. I hope you benefit from viewing code in crisp 4k. 😊
@aar021
@aar021 Жыл бұрын
And I finally got closures
@TrevorSullivan
@TrevorSullivan Жыл бұрын
Nice!!! They're not hard after all, right? 🙂🦀
@Takatou__Yogiri
@Takatou__Yogiri 3 ай бұрын
closure is easy. it's just that rust syntax is hard.
@firstname-lastname
@firstname-lastname Ай бұрын
THIS IS THE BEST CLOSURES EXPLANATION!
@TrevorSullivan
@TrevorSullivan Ай бұрын
@@firstname-lastname I'm very happy to hear that this helped you understand closures! 🦀
@kinositajona
@kinositajona 8 ай бұрын
The IDE filling in phantom types with a similar font and color to the rest of the text was hard to read. add(x: -3, y: -5); gave me an error lol. Maybe keep them off by default and flash them with a hotkey when you want to show what Rust is seeing?
@TrevorSullivan
@TrevorSullivan 8 ай бұрын
Hmmm I'll have to see if that's possible with the Rust Analyzer extension!
@MrMustachehead
@MrMustachehead 2 ай бұрын
I thought the same when I first started with rust, but you get used to it
@piotao
@piotao 10 ай бұрын
Hmm, very nice explanation, but there is perhaps more to be said in this interesting subject. What about function generator with closured variables? What about counters... randomizers... mixers.... and what is FnOnce ??? I think we all would love to see what you can tell about those topics. Closures are quite advanced tbh, but well, that's the time to learn I guess! :)
@MrMustachehead
@MrMustachehead 2 ай бұрын
Thank you Trever, you're an awesome teacher :)
@vonmakeheat
@vonmakeheat 11 ай бұрын
bro im over here nesting closures inside modules .... You are a great tutor Please Dont Stop Making theese videos !!!!
@jakubrembiasz8942
@jakubrembiasz8942 Жыл бұрын
Very well explained. Thank you so much❤
@kelownatechkid
@kelownatechkid 9 ай бұрын
Really straightforward, great video!
@danielrabinovich4813
@danielrabinovich4813 4 ай бұрын
It isn't necessary you answer me, the reason was the annotations I have put textual "impl Fn()" must not be written in the editor, they are VSC Rust Analyzer annotations. Regards
@Guihgo
@Guihgo 4 күн бұрын
28:15 You only can access p1 if you change twice. It's seems an bug, imagine how many bugs will happen with this "feature".. unhappy about rust....
@benjaminpowell1508
@benjaminpowell1508 8 ай бұрын
Trevor, I'm new to Rust and your videos have really helped, thank you. I was just wondering what VS setting you have enabled to get the errors/warnings to show up inline. I think this would be helpful. Thanks!
@TrevorSullivan
@TrevorSullivan 8 ай бұрын
Hey Benjamin, thank you for joining me on the Rust journey! I have a video in the playlist that talks about setting up the Rust development environment. You'll want to install the Rust Analyzer extension for VScode. Keep on learning! 🚀
@benjaminpowell1508
@benjaminpowell1508 8 ай бұрын
@@TrevorSullivan Thanks, I'll check that out!
@AnkurDwivedi-wx9kh
@AnkurDwivedi-wx9kh 5 ай бұрын
@28.00 for who do not understand println macro itself takes immutable reference of value it is supposed to print. hence the error
@ThisGuyDakota
@ThisGuyDakota Жыл бұрын
This is one of those topics I never understood in any language lol (this and recursive functions) I kind of understand how they're used, but no idea what a potential use-case would be so it's hard to practice
@TrevorSullivan
@TrevorSullivan Жыл бұрын
After you spend a while learning the fundamentals, you can add in external libraries that allow you to build rich CLI apps, web APIs, games, and desktop applications. What are you interested in building?
@ThisGuyDakota
@ThisGuyDakota Жыл бұрын
@TrevorSullivan currently don't have a project in mind, just want to learn rust for the sake of learning rust. I'm heavily into PowerShell, and I've dabbled with Python and C# a while ago. Just want to learn some low-level stuff to get a better understanding of what my PowerShell might be doing behind the scenes I guess. If I had to pick a project, I'd say maybe like a pong game or something. Although a CLI tool of some sort might be easier since I wouldn't have to learn gui stuff yet lol
@TrevorSullivan
@TrevorSullivan Жыл бұрын
​@@ThisGuyDakota Nice, I come from a PowerShell background as well. It's awesome, but struggles performance-wise at scale. Knowing how to code in a lower-level language will help you build more efficient apps that scale faster and handle larger volumes of data. Rust (seems) to be the perfect balance of performance and developer friendliness. It's not "easy," but it also guides you along very nicely. 🦀 Rust on! 🦀 Thanks for joining me on the journey. Check out the "ratatui" crate, by the way, for building CLI apps. I'll get around to recording a video about it eventually.
@ZeroCadence
@ZeroCadence 7 ай бұрын
Thank you. It’s a tough language to wrap your head around
@초으-o9t
@초으-o9t 5 ай бұрын
I learned so much. Thank you
@lutfiikbalmajid
@lutfiikbalmajid 10 ай бұрын
Thanks, your tutorial is easy to understand
@Lapatate-s1l
@Lapatate-s1l 21 күн бұрын
That’s nice . But what is the purpose of using the closure if we just run it in the same scope . I really don’t find it useful in this case .
@piyushpatil6874
@piyushpatil6874 7 ай бұрын
Really Good Explanation !
@kamertonaudiophileplayer847
@kamertonaudiophileplayer847 4 ай бұрын
It's more just code inserts which do not have a classical prologue and epilogue of a function.
@CodeXNO-h4i
@CodeXNO-h4i 7 ай бұрын
Well explained
@himerrokowoy3193
@himerrokowoy3193 11 ай бұрын
Brilliant content, thank you!
@j-p-d-e-v
@j-p-d-e-v 10 ай бұрын
Loved the tutorial
@abdushakoor0099
@abdushakoor0099 8 ай бұрын
clear, concise, amazing
@TrevorSullivan
@TrevorSullivan 8 ай бұрын
@abdushakoor0099 Thank you for your kind comment! What other Rust topics are you interested in?
@abdushakoor0099
@abdushakoor0099 8 ай бұрын
@@TrevorSullivan i don't know if this will be out of scope of the content you make but making a TUI with rust kinda stuff.
@TrevorSullivan
@TrevorSullivan 8 ай бұрын
@@abdushakoor0099 That's actually something that I want to do, yes! Thank you for the suggestion.
@abdushakoor0099
@abdushakoor0099 8 ай бұрын
@@TrevorSullivan amazing
@chiruchintha
@chiruchintha 3 ай бұрын
great video man!!
@anjonsarker2374
@anjonsarker2374 Жыл бұрын
Fantastic
@maguilecutty
@maguilecutty 10 ай бұрын
Another banger thanks sgain !!!
@jooonmantv
@jooonmantv 4 ай бұрын
great video thanks
@subhamprasad1373
@subhamprasad1373 Жыл бұрын
so lets say after calling change_name for the first time , then I want to execute so code that requires updated p1.last_name and I want call change_name , so am i not allowed to do that ??
@TrevorSullivan
@TrevorSullivan Жыл бұрын
In that case, one option would be to derive the Clone trait on the object, so you can call .clone() on it. That way, you would have a separate object in memory that can be borrowed independently from the object you're trying to mutate. There might be more efficient solutions than that, but that's the easiest mechanism that I'm currently aware of.
@nhwhn
@nhwhn 8 ай бұрын
Liked and subscribed
Rust Match Expressions and Patterns 🦀
35:51
Trevor Sullivan
Рет қаралды 7 М.
Implement Methods on Rust Structs 🦀
27:27
Trevor Sullivan
Рет қаралды 6 М.
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 118 МЛН
Человек паук уже не тот
00:32
Miracle
Рет қаралды 4,1 МЛН
Which team will win? Team Joy or Team Gumball?! 🤔
00:29
BigSchool
Рет қаралды 15 МЛН
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 77 М.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 140 М.
The purest coding style, where bugs are near impossible
10:25
Coderized
Рет қаралды 1 МЛН
Closures in Rust
19:53
Let's Get Rusty
Рет қаралды 80 М.
Rust Data Modelling Without Classes
11:25
No Boilerplate
Рет қаралды 178 М.
Rust Error Handling - Best Practices
21:33
Jeremy Chone
Рет қаралды 24 М.
From C ➡️ C++ ➡️  Rust
14:06
code_report
Рет қаралды 172 М.
Crust of Rust: Functions, Closures, and Their Traits
1:06:40
Jon Gjengset
Рет қаралды 91 М.
Rust Powered Polymorphism ⚡️ With Traits
9:55
Code to the Moon
Рет қаралды 103 М.
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 118 МЛН