Comments on: A practical guide to async in Rust https://blog.logrocket.com/a-practical-guide-to-async-in-rust/ Resources to Help Product Teams Ship Amazing Digital Experiences Tue, 04 Jun 2024 21:27:45 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Ondřej Hruška https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-5010 Tue, 19 Jan 2021 11:07:23 +0000 https://blog.logrocket.com/?p=20761#comment-5010 The slowwly service seems broken or more “sloww” than intended, so the examples do not really work right now.
You can work around that by requesting e.g. `http://example.com` and sleeping in the appropriate place using `tokio::time::sleep(Duration::from_secs(1)).await;`. Otherwise good introduction!

]]>
By: phara https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-3876 Wed, 30 Sep 2020 11:28:25 +0000 https://blog.logrocket.com/?p=20761#comment-3876 very informative and helpful !!! thanks a lot Carl 🙂

]]>
By: Carl Fredrik Samson https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-3808 Wed, 23 Sep 2020 13:40:38 +0000 https://blog.logrocket.com/?p=20761#comment-3808 Thanks for the feedback, Aksel!

]]>
By: Aksel Gresvig https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-3797 Tue, 22 Sep 2020 14:03:18 +0000 https://blog.logrocket.com/?p=20761#comment-3797 Very cool, Carl Fredrik! Clear and well written.
Keep ’em coming!

]]>
By: Carl Fredrik Samson https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-3343 Fri, 07 Aug 2020 20:21:01 +0000 https://blog.logrocket.com/?p=20761#comment-3343 Thanks for letting me know Taimoor. Glad to hear you found it useful!

]]>
By: Taimoor Khan https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-3339 Fri, 07 Aug 2020 17:00:33 +0000 https://blog.logrocket.com/?p=20761#comment-3339 Thank you soo much Carl for this amazing article, it has helped me a lot to polish my understanding of async Rust.

]]>
By: Carl Fredrik Samson https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-3066 Mon, 06 Jul 2020 20:16:56 +0000 https://blog.logrocket.com/?p=20761#comment-3066 Hi Daniel.

That’s a good suggestion, but I feel the example gets harder to understand using Iterators since we can’t simply unwrap using `?` as we do in the rest of the examples.

`results` is of type: `Vec<Result<Result<(u64, u64), Box, JoinError>>` which is a nested result making the iterator version pretty difficult to understand for people not intimately familiar with functional style programming.

We might as well use `try_fold` instead of `try_for_each` if we were to do this operation using a functional style so the code would look something like this:

“`
let (total_ones, total_zeros) =
results
.into_iter()
.flatten()
.try_fold((0, 0), |(total_ones, total_zeros), res| {
res.map(|(ones, zeros)| (total_ones + ones, total_zeros + zeros))
})?;
“`

I feel it makes things more complicated than it needs to be in an example where the main focus is on keeping the code easy to read for a wide audience without really giving a lot of benefit in return 🙂

]]>
By: Daniel https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-3056 Sat, 04 Jul 2020 23:00:22 +0000 https://blog.logrocket.com/?p=20761#comment-3056 > // Returning errors using `?` in iterators can be a bit difficult. Using a
// simple for loop to inspect and work with our results can often be more
// ergonomic

What’s wrong with `try_for_each`?

]]>
By: Carl Fredrik Samson https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-2988 Mon, 29 Jun 2020 22:53:13 +0000 https://blog.logrocket.com/?p=20761#comment-2988 Hi Philip. I see where the confusion lies now. You see, `#[tokio::main]`is a macro that rewrites `fn main()` in a way that the code ends up looking like the first example.

The difference is that the code you write in a main function with `#[tokio::main]` is wrapped in an async block instead of put in a function called `app` but the end result is pretty similar.

I posted the output from the macro in another answer below but it doesn’t look pretty in the comments section here. If you want to check it out for yourself install `cargo install cargo-expand` and run `cargo expand` in the root of a project with the example code in `main.rs`. You’ll see what it expands into.

]]>
By: Carl Fredrik Samson https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-2985 Mon, 29 Jun 2020 16:58:34 +0000 https://blog.logrocket.com/?p=20761#comment-2985 Glad you enjoyed it. Well, I couldn’t make it too easy 🙂 Seriously, thanks for posting. The next person testing all the code should have a slightly easier time, though.

]]>
By: Carl Fredrik Samson https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-2984 Mon, 29 Jun 2020 16:54:41 +0000 https://blog.logrocket.com/?p=20761#comment-2984 Yes, you’re right. analyze took a “String” in an earlier draft but I changed that without catching this one. Should be fixed soon.

]]>
By: Carl Fredrik Samson https://blog.logrocket.com/a-practical-guide-to-async-in-rust/comment-page-1/#comment-2983 Mon, 29 Jun 2020 16:32:55 +0000 https://blog.logrocket.com/?p=20761#comment-2983 Yeah, you’re right. I was trying to avoid these in this article but it seems I inadvertently introduced it in those examples. I think it’s better to change it to:

“‘
async fn our_async_program() -> Result {
future::ok(“Hello world”.to_string()).await
}
“‘
Since the compiler can infer the rest in this case. Thanks for pointing it out.

]]>