Тёмный

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

Trevor Sullivan
Подписаться 15 тыс.
Просмотров 6 тыс.
50% 1

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.
🤯 Rust Programming Playlist 🦀 • Rust Programming Tutor...
📖 Rust Async Programming Book📦➡️ rust-lang.github.io/async-book
Visual Studio Code ➡️ code.visualstudio.com
Rust Website ➡️ rust-lang.org
Rustup Installer ➡️ rustup.rs
Rust Docs ➡️ doc.rust-lang.org/book
Please follow me on these other social channels!
➡️ trevorsullivan.net
➡️ github.com/pcgeek86
➡️ / pcgeek86
➡️ / trevorsullivan
➡️ / trevorsoftware
➡️ tiktok.com/pcgeek86
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

Наука

Опубликовано:

 

21 сен 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 17   
@TrevorSullivan
@TrevorSullivan 9 месяцев назад
Check out some of these awesome components to upgrade your digital life! Thanks for helping support this channel! 🖥 AMD Ryzen 9 7900x 12-core CPU amzn.to/3EKdkSY 🖥 ASUS TUF Gaming B650-PLUS WiFi Socket AM5 (LGA 1718) Ryzen 7000 ATX Gaming Motherboard amzn.to/460IGAE 🖥 Corsair Vengeance 64GB (2x32GB) DDR5 memory amzn.to/3EMZhw0 🖥 Gigabyte GeForce RTX 4070 12GB Windforce amzn.to/453pIbr 🖥 Samsung 970 EVO Plus 1TB NVMe SSD amzn.to/3PNke0f 🖥 Thermaltake Smart 700W ATX PSU amzn.to/3rkXuuT Storage for cameras & embedded devices 💽 TEAMGROUP A2 Pro Plus Card 512GB Micro SDXC amzn.to/3PLd77l The drone I use ➡ DJI Mini 3 Pro 4k60 Drone amzn.to/3PLd77l My studio video camera ➡ Panasonic Lumix G85 4k amzn.to/3sXoDV7
@symshark
@symshark 9 месяцев назад
Thank you! I've been waiting for this one.
@TrevorSullivan
@TrevorSullivan 9 месяцев назад
Thanks! I will have more coming on the async topic. 🙂 Rust on! 🦀
@shailendrajadhav8603
@shailendrajadhav8603 9 месяцев назад
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.
@prashlovessamosa
@prashlovessamosa 7 месяцев назад
Thanks for everything 🙏. This is playlist more than enough for me Very grateful to you
@farzadmf
@farzadmf 9 месяцев назад
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!
@TrevorSullivan
@TrevorSullivan 9 месяцев назад
Thanks for your feedback! It can be a little bit confusing, I agree. Once you dive into the next video about implementing futures yourself, hopefully it will make a bit more sense.
@farzadmf
@farzadmf 9 месяцев назад
Sure thing; will check that out. This async stuff of Rust is one of those things that hasn't clicked for me at all!
@atreeinthemoon
@atreeinthemoon Месяц назад
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?
@shahidyousuf9464
@shahidyousuf9464 Месяц назад
As per documentation, the macro; pin_mut! pins the Future on the stack. The futures here i.e num1, num2, num3 are pushed onto the stack in the order -> 8, 10, 12 and are popped in reverse order -> 12, 10, 8 irrespective of the sleep time on 12. Even on increasing the sleep time to much higher value, still it prints out in the order -> 12, 10, 8. Obviously due to stack used by pin_mut! macro.
@TheMatttm
@TheMatttm 2 месяца назад
What theme is that?
@TrevorSullivan
@TrevorSullivan 2 месяца назад
Outrun by samrapdev
@natnaelberhane3141
@natnaelberhane3141 2 месяца назад
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"); std::thread::sleep(std::time::Duration::from_secs(5)); println!("num1 completed"); return 8; } async fn num2() -> u8 { println!("num2 started"); std::thread::sleep(std::time::Duration::from_secs(5)); 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"); tokio::time::sleep(std::time::Duration::from_secs(5)).await; println!("num1 completed"); return 8; } async fn num2() -> u8 { println!("num2 started"); tokio::time::sleep(std::time::Duration::from_secs(5)).await; 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.
@TrevorSullivan
@TrevorSullivan 2 месяца назад
Yes great point! Tokio has its own sleep function.
@acatisfinetoo3018
@acatisfinetoo3018 4 месяца назад
Is this the same as multi threading in other languages?
@Gabriel-cv3cw
@Gabriel-cv3cw 4 месяца назад
No it is not. If you were spawning other threads on multiple cores like you would in other languages the various functions would actually run at the same time. For example, if you ran 4 functions each with a 1000 ms sleep multi-threaded then you could expect all functions to complete in around 1000ms together + thread overhead. Async/Concurrent code on the other hand basically passes the baton on a single thread for completing multiple smaller tasks so in the example of the 4 functions with the 1000ms sleep you could expect them to finish in 4000ms instead of 1000ms. Its usefulness is not really shown in real code in this video but basically you could in your main function start 4 different async tasks. When a task completes it queue up more async tasks, which when completed could queue up more async tasks. Because it is a queue and the start of each job is added before a job can complete, all tasks get to start before the first task completes and adds its second part/task. Hope this is helpful, I am learning rust and not very knowledgeable on async rust but I come from js which also has async await concurrency.
@carminex
@carminex 2 месяца назад
No
Далее
Async Rust Is A Bad Language | Prime Reacts
28:46
Просмотров 88 тыс.
Constructors Are Broken
18:16
Просмотров 101 тыс.
Compiler-Driven Development in Rust
13:11
Просмотров 45 тыс.
Creating a Chat Server with async Rust and Tokio
53:44
Gizli Apple Watch Özelliği😱
0:14
Просмотров 4,1 млн