Intro to Rust Async Function Execution With Smol 🦀 Rust Programming Tutorial for Developers

preview_player
Показать описание
The Rust standard crate includes support for defining async functions. Rust async functions return a "Future" type, instead of a direct value. Although you can declare async functions, Rust doesn't include an executor to "drive" those Futures to completion. We need to either develop our own executor (advanced) or use a pre-built executor like tokio, smol, actix, or potentially others. In this video, we'll better understand how async programming works, and contrast it with synchronous and multi-threaded execution. By the end of this video, you should be able to understand how to define an async function and use "smol" as your async executor.

Please follow me on these other social channels!

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names,trademarks and brands does not imply endorsement.

#rustlang #rust #rustdev #opensource #software #linux #devops #programming #rusty #dev #coding #codinglife #code #coder #ubuntu #ubuntulinux #appdev #developer
Рекомендации по теме
Комментарии
Автор

Check out some of these awesome components to upgrade your digital life! Thanks for helping support this channel!

TrevorSullivan
Автор

Another great Rust video by you Trevor. You always explain things very well and very clearly 👍

jasonbraithwaite
Автор

Thank you! I've been waiting for this one.

symshark
Автор

Could you consider making a video to include async and thread spawning? This was a good introduction to async. Thanks for putting in the effort to make a visual presentation.

shailendrajadhav
Автор

Thanks for everything 🙏.
This is playlist more than enough for me
Very grateful to you

prashlovessamosa
Автор

Not to complain about your video (which is SUPERB), but, at least in the languages that I've personally seen, this is THE MOST unintuitive async programming code I've seen!

farzadmf
Автор

what a great video man! thank you thank you!

alexperts
Автор

Thanks for your video. I'm not clear why the thread sleeping longer returns first? when using the futures::select! ? In the output it prints out 12, 10, 8, ideally shouldn't it be 8, 10, 12?

atreeinthemoon
Автор

Is this the same as multi threading in other languages?

acatisfinetoo
Автор

The first part is wrong. Asynchronous programming is not about making progress on multiple tasks so that it feels like progress is made on multiple fronts (or customers), it's about waiting.
Imagine that in part 1 of the first project your worker has to wait a couple of days for materials to come. Doing the task synchronously would mean actually doing nothing while the materials arrive. Doing the task asynchronously would mean switch and start doing other work while the materials arrive.

MateiAndrei
Автор

Hi Trevor. I'm not sure this code is functioning as you intended it to. I tried similar functions but with print statements and it seems like the code is executed synchronously.

use futures::executor::block_on;
use futures::join;

async fn num1() -> u8 {
println!("num1 started");

println!("num1 completed");
return 8;
}

async fn num2() -> u8 {
println!("num2 started");

println!("num2 completed");
return 10;
}

fn main() {
let _ = block_on(async { join!(num1(), num2()) });
}

The output is:

num1 started
num1 completed
num2 started
num2 completed

The issue seems to be the use of `std::threads::sleep`. I got the warning "Blocking `sleep` function cannot be used in `async` context". To turn the code into async, the compiler suggested replacing it with `tokio::time::sleep`.

Changing the code to this solved the issue.

use futures::executor::block_on;
use futures::join;

async fn num1() -> u8 {
println!("num1 started");

println!("num1 completed");
return 8;
}

async fn num2() -> u8 {
println!("num2 started");

println!("num2 completed");
return 10;
}

#[tokio::main]
async fn main() {
let _ = block_on(async { join!(num1(), num2()) });
}

output:
num1 started
num2 started
num1 completed
num2 completed

which shows the code is executed asynchronously.

natnaelberhane