Rust: Closures

  Рет қаралды 17,245

The Dev Method

The Dev Method

Күн бұрын

Пікірлер: 27
@mgdotdev
@mgdotdev 2 жыл бұрын
We're kind of getting definitions mixed up here, the || -> {} is just an anonymous function. A closure is a higher-level construct which uses a function scope to create state. So for example, fn counter() -> impl FnMut() -> i32 { let mut val = 0; let _counter = move || -> i32 { let current = val; val += 1; return current; }; return _counter; } this is a closure. calling counter() creates a function scope that holds a `val` in scope, and then subsequent calls to the returned anonymous function mutates that state, without exposing it directly.
@Yotanido
@Yotanido Жыл бұрын
In Rust parlance, all functions are called closures, whether they actually close over anything or not. I'm personally not a big fan of this, either. I guess the reason is that they can be closures, so even if they don't take anything into scope, you could argue they are a kind of trivial closure? Yeah, I don't know. I've gotten used to it now.
@flippert0
@flippert0 10 ай бұрын
Ah, yes, this would be the classical example for a closure. You can realize a .counter withouth making it global and without exposing "val" (which has gone out of scope at the time "counter()" returns. I guess, the Rust developers called everything with the "|| -> {}" syntax a closure, even if no variable to close over is involved.
@cerulity32k
@cerulity32k 2 жыл бұрын
I love how closures are so flexible with their verbosity. You can have ||0 if you just want a zero, or you can have |min: f32, max: f32| -> f32 { // a bunch of code }. It makes them so usable.
@snk-js
@snk-js 2 жыл бұрын
i am addicted to your lectures, thanks, sir
@MickAlpine
@MickAlpine 2 жыл бұрын
I'm loving your rust series! One request is if you could run your examples so we can see the output (if applicable), not just the code. I also love your VS Code theme (even your older videos had a great theme), which one is it?
@nouaimmebarki5669
@nouaimmebarki5669 Жыл бұрын
Hello @TheDevMethod please can you explain why you were testing if one of the lists can be printed before the other in 11:42 isn't it that they are run by their lines order in the code?
@br3nto
@br3nto Жыл бұрын
11:13 two things with move: so we can’t have two threads operating on the same struct? What if we have more than one struct, in the closure… will they all be moved, or can we explicitly specify ownership for each variable in the closure?
@nicolaslopeza2251
@nicolaslopeza2251 2 жыл бұрын
At minute 11:50 you are considering that you might get the output in reversed order. Is that possible? Since you are first using println and then creating the thread, i think that it shouldn't change. I understand that if you first created the spawned thread and then you had a print on the main thread, you might have the output in a random order. Of course in that case you should use the same var because of the error you mentioned earlier. Great content, keep it going!
@TheDevMethod
@TheDevMethod 2 жыл бұрын
You are correct. We would never get a different order. The example should have been a println after spawning the thread. Thanks for catching this!
@prashlovessamosa
@prashlovessamosa 2 жыл бұрын
Great explanation man
@br3nto
@br3nto Жыл бұрын
8:44 I wonder why the compiler doesn’t do this same thing for inferred types for closures… eg show the line which infers the type, along with the line with the type mismatch.
2 жыл бұрын
great explanitation! thank you
@sunkeet230
@sunkeet230 Жыл бұрын
is there a mistake at 9 20?
@simis_tld
@simis_tld 2 жыл бұрын
code block inside the braces highlightting, is it a plugin or any specific styling?
@TheDevMethod
@TheDevMethod 2 жыл бұрын
I used Synth Wave '84 theme extension by Robb Owen
@tourdesource
@tourdesource 2 жыл бұрын
When would you opt for a regular function instead of a closure?
@toufxiong83
@toufxiong83 Жыл бұрын
Im still confused...can you step through a function so we can actually see how it works. You go from one example of shirts, to another example of adding 1 without actually seeing what a closure does. I learn best by stepping through a function. Thanks!!!
@draakisback
@draakisback Жыл бұрын
Nice video. I really dislike how rust handles functions and closure types. I know I'm biased because I am a mostly functional programmer but still...
@irlshrek
@irlshrek 2 жыл бұрын
Love the rust content!!
@adicide9070
@adicide9070 2 жыл бұрын
yo it is an arrow pointing to the right :D
@kovacsemod
@kovacsemod 2 жыл бұрын
I watched the whole 20 minutes waiting for you to show a closure 😢
@andre_julius
@andre_julius 2 жыл бұрын
Thank for the video! I've already read theiught the book and kinda grasped fn traits but hearing other people explain it always helps me understand it better :D With that said, I still have problems combining these Fn traits with async rust. For example if I want to have an sync fn that can take an async fn as an argument, things get weird haha. Do you have problems with that as well?
Rust: Iterators
20:07
The Dev Method
Рет қаралды 15 М.
How to Get a Developer Job - Even in This Economy [Full Course]
3:59:46
freeCodeCamp.org
Рет қаралды 3,1 МЛН
小路飞还不知道他把路飞给擦没有了 #路飞#海贼王
00:32
路飞与唐舞桐
Рет қаралды 82 МЛН
Увеличили моцареллу для @Lorenzo.bagnati
00:48
Кушать Хочу
Рет қаралды 8 МЛН
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 16 МЛН
Happy birthday to you by Secret Vlog
00:12
Secret Vlog
Рет қаралды 6 МЛН
Rust Structs: Defining and Using Custom Data Types
2:06
Learn Rust
Рет қаралды 40
Understanding Rust Closures aka. Anonymous Functions 🦀 💻
30:22
Trevor Sullivan
Рет қаралды 16 М.
Rust: RefCell
20:00
The Dev Method
Рет қаралды 8 М.
Closures in Rust
19:53
Let's Get Rusty
Рет қаралды 80 М.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 140 М.
Choose the Right Option
18:14
Logan Smith
Рет қаралды 73 М.
Rust Data Modelling Without Classes
11:25
No Boilerplate
Рет қаралды 179 М.
Rust: Weak References
19:40
The Dev Method
Рет қаралды 4,1 М.
Rust for TypeScript Developers by ThePrimeagen | Preview
14:59
Frontend Masters
Рет қаралды 98 М.
Crust of Rust: Functions, Closures, and Their Traits
1:06:40
Jon Gjengset
Рет қаралды 91 М.
小路飞还不知道他把路飞给擦没有了 #路飞#海贼王
00:32
路飞与唐舞桐
Рет қаралды 82 МЛН