Crust of Rust: Functions, Closures, and Their Traits

  Рет қаралды 83,989

Jon Gjengset

Jon Gjengset

Күн бұрын

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
Live version with chat: • Crust of Rust: functio...

Пікірлер: 87
2 жыл бұрын
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.
@EidosX_
@EidosX_ 2 жыл бұрын
He is my favorite learning resource on KZbin, Jason Turner close second (c++) and Ben Awad third (web)
@malborboss
@malborboss 7 ай бұрын
Zgadzam się. Koleś mega mądry i do tego potrafi wytłumaczyć
@teasysneeze
@teasysneeze 2 жыл бұрын
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.
@rishadbaniya
@rishadbaniya 2 жыл бұрын
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
@thesuperyou2829
@thesuperyou2829 2 жыл бұрын
He is a PHD student and have made a database in rust as PHD thesis... so he is very qualified
@chrisfredrickson1085
@chrisfredrickson1085 2 жыл бұрын
@@thesuperyou2829 He's not a student anymore, he's already received his PhD and graduated. Now he's working in industry.
@KevinDay
@KevinDay 2 жыл бұрын
I was JUST the other day wishing you had a video on closures 😂
@TroenderTass
@TroenderTass 2 жыл бұрын
You know, everything is in the official rust book wich is on their offical page. You would think someone who wanted to learn such a niche language would take the time to go trough the proper docs.
@KevinDay
@KevinDay 2 жыл бұрын
@@TroenderTass You'd think someone who wanted to troll a community would take the time to come up with a better comment.
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 2 жыл бұрын
Definitely the best Rust content on KZbin 👍
@mazup1
@mazup1 2 жыл бұрын
Preordered the book. I already know I will get $25 worth of value. I already get that with the KZbin channel!
@cat-.-
@cat-.- 2 жыл бұрын
Like, each of his crust of rust video is worth $25 already, probably $100 if it's taught in a uni
@aqtmeto
@aqtmeto 2 жыл бұрын
@@cat-.- This is really true.
@beastle9end499
@beastle9end499 2 жыл бұрын
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
@thesuperyou2829
@thesuperyou2829 2 жыл бұрын
Great suggestion
@philosophyze
@philosophyze 2 жыл бұрын
check out Let's Get Rusty's short video on OOP vs Traits kzbin.info/www/bejne/o5DTmZeModClbLc
@Gr8putin
@Gr8putin 2 жыл бұрын
Great suggestion whenever I see gtk-rs or some rust code I feel examples should be using oops more
@michalbotor
@michalbotor Жыл бұрын
my guess would be: structs, traits, and composition.
@alejandrolobo1485
@alejandrolobo1485 2 жыл бұрын
I really enjoy your explanations. Well structured, technical and deep explanations! Thanks
@daltonyon
@daltonyon Жыл бұрын
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
@dimitardimitrov3421
@dimitardimitrov3421 Жыл бұрын
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!
@cerulity32k
@cerulity32k Жыл бұрын
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.
@dabrick100
@dabrick100 2 жыл бұрын
You're such an amazing teacher
@rezakh
@rezakh 2 жыл бұрын
Thanks for all your recording.
@zhengren8580
@zhengren8580 11 ай бұрын
The contents of this channel is by far the best for Rust.
@antonyjr.devlogs5957
@antonyjr.devlogs5957 2 жыл бұрын
RUST IS SO INTERESTING AND EXCITING.
@Encysted
@Encysted Жыл бұрын
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".
@distrologic2925
@distrologic2925 Жыл бұрын
The value of your content will echo through eternity ;)
@norliegh
@norliegh 9 ай бұрын
Jon, thank you! This was very helpful.
@muhamedadel5859
@muhamedadel5859 9 ай бұрын
Your videos are so good that I sometimes forget to click the Like button. Thanks.
@patix0331
@patix0331 2 жыл бұрын
Loving these videos. You are great teacher, keep it up.
@adicide9070
@adicide9070 11 ай бұрын
one of my faves in the series :)) this and collections, the new one.
@fernanboxfrias381
@fernanboxfrias381 2 жыл бұрын
I love your videos. One question, could you do videos more often?
@birraasmile9722
@birraasmile9722 2 жыл бұрын
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.
@MrNathanShow
@MrNathanShow 2 жыл бұрын
Just ordered the book for reference, thank you 👍
@pinnaclesystemsgroup6472
@pinnaclesystemsgroup6472 2 жыл бұрын
Jon - Thanks so much for this instructive video. Clearly, the rich and challenging concepts you demonstrate in Rust require careful consideration and exploration before use. Closures are still a challenge to fully comprehend, not because you video lacked anything, but simply because the Rust idiom requies one to carefully design the program's call flow. As an aside, would you be willing to share your .vimrc file and the plug-ins you are using?
@japrogramer
@japrogramer 2 жыл бұрын
Are you going to implement Arc dyn Fn ? And can you check for ~const with a trait?
@drcx3
@drcx3 2 жыл бұрын
Unfortunately, sending the book to Europe would cost almost like the book itself, but hey, that's what the ebook version is for ;) ! Thanks for all the content, hope buying the book will help you getting even more contents out!
@mateuszkubaszek7318
@mateuszkubaszek7318 2 жыл бұрын
Thanks for your video. I notice that you move from xmonad :) What is your current tailing windows manager. And is it LeftWM?
@fbytgeek
@fbytgeek 2 жыл бұрын
Hey Jon - question on something unrelated to this video, does any of your long videos show graph implementation?
@alphabitserial
@alphabitserial 2 жыл бұрын
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.
@flashbangstudio9102
@flashbangstudio9102 2 жыл бұрын
I realy enjoy all the explenations and thank you for that great job, but it's hard for me to figure out with all this extremly abstract exemples.
@narekasadorian
@narekasadorian Жыл бұрын
Do you use neovim LSP or CoC? The inlined error reporting looks really nice…
@jensmeindertsma7526
@jensmeindertsma7526 2 жыл бұрын
How does he get the error message highlight in vim to look like that?
@CPTSMONSTER
@CPTSMONSTER 29 күн бұрын
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) 46:00 The wrapper type (indirection type) needs to allow access to dyn Fn*: &dyn Fn(), &mut dyn FnMut(), Box 47:45 Arc always gives shared access, so Arc 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
@srikarsana7146
@srikarsana7146 2 жыл бұрын
i have a small doubt. you said Fn implements both Fnmut and Fnonce when we passed closure to the Fn where it needed mutable access there was an error. So what ever is Fn should have Fnmut and Fnonce isnt it ? or did i misunderstood the explanation
@jeffg4686
@jeffg4686 2 жыл бұрын
Is the whole point of "as_str()" that, if you didn't call it, the string would be consumed upon calling a method, or does it have more to do with the Slice trait having those methods that you are calling - such as len()?
@emdadgar_official
@emdadgar_official 2 жыл бұрын
Fn, FnOnce, FnMut ,,,, i wanna understand these ... and differences between FnOnce and "move" keyword :( any resource ?
@shashanksharma21
@shashanksharma21 2 жыл бұрын
What a legend !! 🙇‍♂️
@ismaelelalaoui5270
@ismaelelalaoui5270 2 жыл бұрын
Can you please share which vim plugin you are using in the video? Thanks in advance.
@jacklong2182
@jacklong2182 2 жыл бұрын
thanks for this tutorial
@genericdeveloper3966
@genericdeveloper3966 2 жыл бұрын
50:00 Maybe I'm just dumb, but I can't for the life of me think of the utility of a const fn expression. Since it can be evaluated at compile time couldn't we just replace the const fn with whatever const value it is evaluating to?
@jonhoo
@jonhoo 2 жыл бұрын
Sometimes you can, but that doesn't work for, say, constructing types that are defined outside of your crate (since you can't name their private fields). Sometimes it's also nice to express the computation rather than the final result. As a trivial example, would you rather have a constant with the value 2592000 or one defined as 30*24*60*60?
@jeffg4686
@jeffg4686 2 жыл бұрын
13000 views - I'd say Rust is picking up some steam. Nice video. I had one question if you don't mind - when calling the closure, why is the parentheses around the variable needed, such as "(f)()" instead of just "f()"
@jonhoo
@jonhoo 2 жыл бұрын
Ah, imagine there's a fn f() {} in the same scope - it wouldn't be clear which to call. (f)() indicates to call the function indicated by the *value* f, not the *name* (technically function item type) f.
@jeffg4686
@jeffg4686 2 жыл бұрын
​@@jonhoo - thanks
@jeffg4686
@jeffg4686 2 жыл бұрын
Jon, you need to sell some kinda cheat sheet. Rust has too many little "isms". Too hard to remember. But I guess a rust cheat sheet would be whole book, so ... COR says "steal a value from a borrow and replace it with another". Audience looks at each other. That's rust though.
@VivekYadav-ds8oz
@VivekYadav-ds8oz 2 жыл бұрын
Are there any updates on _impl Fn for Arc_ and why it hasn't been implemented yet?
@jonhoo
@jonhoo 2 жыл бұрын
twitter.com/jonhoo/status/1447671516041121794?t=aavlfVsytTuCtTzcDQ1dXg&s=19
@jmoo4457
@jmoo4457 2 жыл бұрын
I know this is off topic but can you share your development setup?! I love the color scheme and all (terminal, vs code, extensions, etc.)
@Baremutation
@Baremutation 2 жыл бұрын
kzbin.info/www/bejne/r5SwmoB6fdSjhMU
@jmoo4457
@jmoo4457 2 жыл бұрын
@@Baremutation amazing. Thanks !!
@xrafter
@xrafter 2 жыл бұрын
There was an audio problem in this stream . Check obs and your configurations probably changed after the update
@jonhoo
@jonhoo 2 жыл бұрын
Yup, I'm aware - the VoD should have the audio glitches edited out. This isn't normally the case, so I suspect it's related to a recent upgrade.
@kehindefasunle4933
@kehindefasunle4933 2 жыл бұрын
I can't just stop to think "How does he know so much?" I really appreciate this guy ❤️💯
@tvboyd23
@tvboyd23 2 жыл бұрын
What color scheme you using in vim?
@furryboffin
@furryboffin Жыл бұрын
I am facing an error that has me stumped. higher-ranked lifetime error could not prove `[async block std::marker::send. This is on the tokio::spawn closure. When i try to capture an Arc.
@darkarie
@darkarie 2 жыл бұрын
What firefox css its than one? :)
@ClearerThanMud
@ClearerThanMud 2 жыл бұрын
Would it make sense to put a link to your book in the description?
@jonhoo
@jonhoo 2 жыл бұрын
Ah, probably, although I feel like I've been posting it _everywhere_ recently :p It's also pretty easy to find since it should be on most retailers and such. nostarch.com/rust-rustaceans is the one you probably want.
@KhaledKimboo4
@KhaledKimboo4 Жыл бұрын
I've never imagined I'd have such a hate/love relationship with programming language
@ewhac
@ewhac 2 жыл бұрын
11:37: It's 'quux'. But I'm not fond of it myself, so I usually use 'grill' :-).
@itellyouforfree7238
@itellyouforfree7238 2 жыл бұрын
I use asd qwe zxc fgh rty vbn etc...
@albedobond3827
@albedobond3827 2 жыл бұрын
i didn't know that variables can take fn like javascript lol I didn't even think about using it
@AlexanderKrivacsSchrder
@AlexanderKrivacsSchrder 2 жыл бұрын
I'd love to get the physical book, but the shipping is nearly as expensive as the book, and that's just not right. It's so unfair how Americans can get something for like $5 in shipping and I have to pay $25-45 for the same thing.
@jonhoo
@jonhoo 2 жыл бұрын
Yeah, the international shipping cost is pretty unfortunate. The good news is that it'll be available at local retailers too eventually (I'm guessing end-of-year, but depends on the distributor), and then it should be much easier to get a hold of!
@AlexanderKrivacsSchrder
@AlexanderKrivacsSchrder 2 жыл бұрын
@@jonhoo Awesome. I'll keep an eye on the situation!
@kylidboy
@kylidboy 2 жыл бұрын
A fan from China, it's really difficult to buy the book
@jonhoo
@jonhoo 2 жыл бұрын
It should become available at local retailers too eventually, and then it should be much easier! I'd guess around the end of the year.
@hecalvin1659
@hecalvin1659 2 жыл бұрын
This topic is difficult to understand for me. A little wired feature doesn't exist in other languages
@Randych
@Randych 6 ай бұрын
No need to advertise the book I already bought!
@xrafter
@xrafter 2 жыл бұрын
Name it function-items Or fn-items
@monirahmadi9605
@monirahmadi9605 2 жыл бұрын
your screen is too small :)))) with 17 inch monitor ,,, please brooo
@bezcisla
@bezcisla 2 жыл бұрын
It's crazy syntax. Its like assambler. Nothing intuitive.... _: fn :: std mem etc... For someone who can program, there are some points what they can see, but for absolutly new programmer it must be demotivation to learn something.
@Gammaglobulino
@Gammaglobulino 2 жыл бұрын
_ is a wildcard same as other languages, it means you can stick everything, :: what is called the "turbofish" means you coerce somehting to what is contained inside the TB in this case an i32, fn is a pointer to a function. Not a crazy syntax at all if you carefully study the doc. Ciao
@yapayzeka
@yapayzeka Жыл бұрын
this is not teaching. this is litterally showing off
Crust of Rust: Dispatch and Fat Pointers
2:12:52
Jon Gjengset
Рет қаралды 80 М.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 124 М.
어른의 힘으로만 할 수 있는 버블티 마시는법
00:15
진영민yeongmin
Рет қаралды 8 МЛН
小路飞姐姐居然让路飞小路飞都消失了#海贼王  #路飞
00:47
路飞与唐舞桐
Рет қаралды 94 МЛН
1 класс vs 11 класс (неаккуратность)
01:00
БЕРТ
Рет қаралды 4,6 МЛН
How to implement a signature pad in WeWeb
14:00
Jean-Philippe Trinh
Рет қаралды 65
Crust of Rust: Iterators
1:26:27
Jon Gjengset
Рет қаралды 94 М.
Decrusting the serde crate
1:51:21
Jon Gjengset
Рет қаралды 51 М.
Rust Generics and Traits: Define Common Struct Behaviors 🦀
32:23
Trevor Sullivan
Рет қаралды 3,6 М.
Traits and You: A Deep Dive - Nell Shamrell-Harrington
22:09
Rust Belt Rust Conference
Рет қаралды 38 М.
A Firehose of Rust, for busy people who know some C++
1:24:12
Jack O'Connor
Рет қаралды 80 М.
Rust: Closures
19:53
The Dev Method
Рет қаралды 16 М.
Crust of Rust: Subtyping and Variance
1:39:41
Jon Gjengset
Рет қаралды 57 М.
Generic Types in Rust
15:18
Let's Get Rusty
Рет қаралды 64 М.
Huawei который почти как iPhone
0:53
Romancev768
Рет қаралды 515 М.
👎Главный МИНУС планшета Apple🍏
0:29
Demin's Lounge
Рет қаралды 490 М.
How much charging is in your phone right now? 📱➡️ 🔋VS 🪫
0:11
С Какой Высоты Разобьётся NOKIA3310 ?!😳
0:43