Intro
If you are a programmer and you like Rust you might be aware of the future movement that is buzzing around the Rust community. G...
For further actions, you may consider blocking this person and/or reporting abuse
Why do you need the lambda?
why not?
You do not need the lambda in this case, your code will work just fine. I used the lambda for two reasons:
Blog post logic
In the post I was migrating step-by-step from a non-async code to an async one. In the previous step I had this code:
The logical following step was to chain those statements into one, so I kept the same binding names (
retval
andretval2
):IMHO hiding the
retval
value would have been harder to follow.Taste
I generally prefer to use closures as they are more explicit. Let me draft an example.
If we take into account these three
Future
returning functions:The first one takes no parameters, the second one just one and the third one two.
You can chain the first two as you did. That's the functional approach:
That is, the parameter is implicitly passed between the futures. But what about chaining the first and the third future? This will not work because the third future expects two parameters:
It will give, unsurprisingly:
You cannot curry the function either:
As Rust will complain like this:
For this reason I prefer the closures: the above code can be expressed like this:
It's more verbose but in this case I like the explicitness more. Also Rust gives you control if you want to
move
the captured variables so you have control over that too.In the end I think it's a matter of taste (I don't know if there is speed difference so maybe one solution is better than the other in that regard).
Hi Francesco. Thanks for the tutorial!
Could you please explain why do you use
my_fn_squared
and notmy_fut_squared
in the code below?Hi Andrei,
although there is no difference in the result between these two:
The signature is different:
That said I think you are right, the code I used in the post is misleading. I will correct it right away!
Thank you very much,
Francesco
Given that futures are part of standard now, so you think learning tokio will still be a useful skill or should one spend their energy trying to figure out the new standard? (Which looks pretty complex)
Most of the stuff above still applies to the "new" features (but there are some differences like the
Pin
type) so it can be useful to read it, especially because combinators are still allowed.That said, the
await
syntax is the future so you will have to study it anyway. You might as well start from there and save some time.It's still Rust though: what seems complex usually is! I plan to write about the new standard when the dust settles (and when I'm knowledgeable about it, which may never happen :))!
Thank you for taking the time to write about futures! The post is really good and meets his expectations. May be you should consider writing a part of The Book on futures with the help of Alex Crichton
Thank you for reading it! I will contact Alex Crichton and see if it makes sense to include it in the book.
The point is I would like these posts to be more relaxed in terms of both writing style and lexical accuracy: Rust tends to be intimidating as it is :). Take the official future tutorial for example: it's technically excellent but it starts with the why instead of how. An average programmer, such as me, might want to start with small steps instead of being thrown directly into the intricacies of the asynchronous programming.
Thank you so much.❤️