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:
error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
--> src/main.rs:141:27
|
141 | let retval2 = reactor.run(chained_future_two).unwrap();
| ^^^ expected function that takes 1 argument
|
= note: required because of the requirements on the impl of `futures::Future`for`futures::AndThen<impl futures::Future, _, fn(u32, u32) -> impl futures::Future {my_fut_two}>`
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).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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).