Red

Red

Rust Brain Teasers: Comments on puzzle 19/async (page 82-)

An open discussion about this particular puzzle on asynchronous code, since I’m only learning and I may have overlooked something.

1.) About tokio, you either have to declare the main as

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { ... }

or if main is something else, it also possible to use its own block_on (or other alternative) to launch the asynchronous code. In this case, you have to enable the time “reactor” to avoid a panic:

fn main() {
    let rt = runtime::Builder::new_multi_thread()
        .enable_time()
        .build()
        .unwrap();

    rt.block_on(test()).unwrap();
}

with, for example

    async fn test() -> Result<(), Box<dyn std::error::Error>> {
        join!(count_and_wait(1), count_and_wait(2), count_and_wait(3));
        Ok(())
    }

or directly inserting an async block: rt.block_on(async { ... }).

2.) There is another alternative to using tokio::time::sleep or a thread-oriented approach.

The problem comes from using a thread function, which is not meant for this sort of asynchronous pattern as very well explained pages 82 and 85. We actually get the same issue in other languages like Kotlin. There is no .await and so no real “break” that will allow to switch to another async code.

There is an asynchronous async_std::task::sleep function which supports .await and will give the expected outcome:

    use async_std::task;

    async fn count_and_wait(n: u64) -> u64 {
        println!("starting {}", n);
        task::sleep(Duration::from_millis(n * 100)).await;
        println!("returning {}", n);
        n
    }

Where Next?

Popular Pragmatic Bookshelf topics Top

New
jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New

Other popular topics Top

AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Sub Categories: