Crust of Rust: Functions, Closures, and Their Traits

preview_player
Показать описание
In this episode, we go over the differences between function items, function pointers, and closures, as well as how they interact with the Fn* traits. We also touch upon dynamically dispatched Fns and experimental const Fn bounds.

I've spliced out some audio issues that occurred on the live-stream, but hopefully it won't be too noticeable :p

0:00:00 Introduction
0:01:20 Rust for Rustaceans
0:02:48 Function items
0:06:26 Function pointers
0:11:24 Function traits
0:20:40 Closures
0:33:49 Non-static closures
0:38:50 dyn Fn
0:49:44 const Fn
1:00:28 for bounds
1:04:06 closures in async fn

Рекомендации по теме
Комментарии
Автор

I might not have the most experience in the world but I've gone through 5 programming languages that I feel somewhat comfortable in, along with all their ecosystem, communities, documentations, tutorials etc. Just wanted to say you sir are making the best programming learning resources I've seen so far. You are truely gifted, thank you so much for sharing your knowledge. Cheers from Poland.

Автор

I didn't realize you wrote Rust for Rustaceans. I preordered a physical copy last month, before finding this channel; I was pretty excited for it to arrive. After finding your exceedingly informative channel, I'm now very very excited for the text version! Thanks for filing this niche.

teasysneeze
Автор

I just found your channel yesterday as I have started learning rust a few weeks ago. Good thing I found you early, your explanations are very good, I could understand everything perfectly. If I am not sure about something, I just go back to your explanations, and it just clicks. Thank you for making your videos available for us. I haven't watched all your videos yet, but I plan to keep watching until I fully absorb everything you shared.

thedudegotfourviews
Автор

Definitely the best Rust content on YouTube 👍

climatechangedoesntbargain
Автор

If I ever manage to get a Rust job it will be 90% because of you. I’ve learned so much through your amazing videos and streams!

dimitardimitrov
Автор

Great video, since I came from C ++ to Rust, it's just great for me to see videos like this at this quality level! I have a suggestion for a possible future video: Since Rust has no inheritance like many languages like Java / C ++, you could possibly make a video showing how object-oriented solutions are made differently in Rust, that would be interesting

beastleend
Автор

I love Rust's implementation of closures since not only is it easier to distinguish from functions/function calls, but if you just want to return a value (like in unwrap_or_else), you don't need ()=>{return 0;}, you just need ||0, making it so much more concise.

cerulityk
Автор

I was JUST the other day wishing you had a video on closures 😂

KevinDay
Автор

The contents of this channel is by far the best for Rust.

zhengren
Автор

People are asking for "teach me this teach me that", i just wanna know how you learn all these things. how do you read docs?. How long does it take in average to say "ah..i got it".Consider creating a video on learning a new topic u are not familiar with in rust in live stream. Just like teaching a person "how to catch and eat a fish" rather than "how to eat a fish".

P.S. Thanks for making these videos, they are great learning material

rishadbaniya
Автор

Preordered the book. I already know I will get $25 worth of value. I already get that with the YouTube channel!

mazup
Автор

Rust and the teaching tools around it have finally helped me escape the "writing bad, unflexible scripts" stage of my programming career. Thanks Jon for all your incredible videos! Can't wait to check out your book.

alphabitserial
Автор

7:20 Function items are coercible into function pointers
18:30 FnOnce, FnMut, Fn hierarchy (self can be turned into &mut self and &self, &mut self can be turned into &self)
19:15? A function pointer implements Fn, and therefore also implements FnMut and FnOnce
22:00 Closures which do not capture over their environment are coercible to function pointers (fn type)
28:45 Passing an FnMut into quox (mut in signature)
30:30 So if a closure that implements the FnOnce trait is expected, then FnMut and Fn can be input. If FnMut is expected, then Fn can be input but not FnOnce. (See note at 18:30)
32:00? The compiler can deduce if a closure requires move, but there are edge cases i.e. when dropping a value in the closure
35:20 If move is not specified, the value would be borrowed from the environment's scope and require a lifetime
37:00? Shadowing
42:20 dyn is not Sized, so there's always a reference/exclusive reference to it or Box around it (history on standard library implementation of Fn* for Box<dyn Fn*>)
46:00 The wrapper type (indirection type) needs to allow access to dyn Fn*: &dyn Fn(), &mut dyn FnMut(), Box<dyn FnOnce()>
47:45 Arc always gives shared access, so Arc<dyn FnMut()> still only implements Fn (missing implementation for Arc!)
1:02:30 for syntax to specify lifetimes in Fn* trait
1:05:00? static in async

CPTSLEARNER
Автор

The value of your content will echo through eternity ;)

distrologic
Автор

I think a good substitute for "advanced" in things like "advanced topics" is to lean on the layering model of knowledge, where "this knowledge requires having mental frameworks already in place (from having learned previous topics) in order to make sense of it and intuit its use".

Encysted
Автор

Jon awesome class, its the third time that I'm see again and always learning something new!!! Thank u very much to spend your time with this incredible class

daltonyon
Автор

I really enjoy your explanations. Well structured, technical and deep explanations! Thanks

alejandrolobo
Автор

one of my faves in the series :)) this and collections, the new one.

adicide
Автор

Your videos are so good that I sometimes forget to click the Like button. Thanks.

muhamedadel
Автор

Jon, thank you so much for resources that you have been sharing for free for the Rust community or anyone interested. Your contents pulled me to Rust and am investigating time to learn it better.

birraasmile
visit shbcf.ru