Тёмный

Rust Scoped Threads 🦀 Rust Tutorial 

Trevor Sullivan
Подписаться 17 тыс.
Просмотров 4,9 тыс.
50% 1

Rust provides a concept known as "scoped threads" in the standard library. You can use scoped threads to borrow heap-allocated memory, from the parent scope, and then return control back to the parent scope. If you don't use scopes to spawn threads, you will most likely run into issues with borrowing variables from the parent scope.
🤯 Rust Programming Playlist 🦀 • Rust Programming Tutor...
📖 Rust std::thread::scope docs ➡️ doc.rust-lang....
Visual Studio Code ➡️ code.visualstu...
Rust Website ➡️ rust-lang.org
Rustup Installer ➡️ rustup.rs
Rust Docs ➡️ doc.rust-lang....
Please follow me on these other social channels!
➡️ trevorsullivan...
➡️ github.com/pcg...
➡️ / pcgeek86
➡️ / trevorsullivan
➡️ / trevorsoftware
➡️ tiktok.com/pcg...
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

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

 

27 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 16   
@TrevorSullivan
@TrevorSullivan Год назад
Check out the FULL Rust playlist 🦀🦀🦀 ru-vid.com/group/PLDbRgZ0OOEpUkWDGqp91ODn0dk7LPBAUL
@shahidyousuf9464
@shahidyousuf9464 4 месяца назад
Your channel is definitely underrated!!! Deserves 100k subscriptions by now and more.
@Scotthutchinsonking
@Scotthutchinsonking 2 месяца назад
100% agree good sirs
@FourTwentyMagic
@FourTwentyMagic Год назад
Pretty sure structs are also stack allocated, Altho the first_name String would be heap-allocated
@davidjdoherty
@davidjdoherty 3 месяца назад
Yeah. Was thinking the same. Heaps are less efficient in other languages because of the implementation. I understand his comment on performance
@CrapE_DM
@CrapE_DM Месяц назад
Yeah, his explanation is only a tiny part of the explanation. BECAUSE it's a SMALL stack allocation, they've marked i32 to implement Copy, which allows for copies when being moved. So, implement the Copy trait to make moves act like cloning
@paralaxa
@paralaxa 14 дней назад
one of best youtube teacher. tnx fot content ! :*
@ians.2860
@ians.2860 6 месяцев назад
This video was really helpful, thank you!
@AhmedFalih-kj3tt
@AhmedFalih-kj3tt Год назад
why cant use in the first example of (spawn of threads) removing the move key word and add & into person... and could you create video about smart pointers Mutex, Arc, Cell, OnceCell, OneCell, etc... btw i really like you videos. ♥
@TrevorSullivan
@TrevorSullivan Год назад
Good question! If you don't use a "move", you'll get an error saying "closure may outlive the current function, but it borrows person01.first_name, which is owned by the current function." That's because the thread could possibly live *longer* than the function that spawned the thread, but the thread borrowed that value from the function. It's a sort of race condition issue, that's solved by doing a "move" of ownership into the thread. If you call .join() on the thread, you ensure that the thread will NOT outlive the function, but Rust still requires that you move ownership, or use scoped threads, which *guarantees* joining with the main thread.
@AhmedFalih-kj3tt
@AhmedFalih-kj3tt Год назад
thank you man for these videos, they are really helping me out ❤
@ERAYKAAN850
@ERAYKAAN850 Год назад
didn't watch it but nice video anyways (:
@peterhayman
@peterhayman 7 месяцев назад
great quality tutorials, thank you!
@fsaldan1
@fsaldan1 10 месяцев назад
In your examples the threads only take pre-existing data snd print it. I am sure they could also calculate something new and print it. The question is: could they calculate something new and pass it on to the main thread that would then use it. If that is possible, how would one do that?
@TrevorSullivan
@TrevorSullivan 10 месяцев назад
Check out the video that covers mutexes, and the video that talks about MPSC (message passing). Those videos cover a couple of ways that you can share data between threads.
@pooyannajafi
@pooyannajafi 8 месяцев назад
With scope and after removing "move" I could also use "person01.name" without any "&" in the closure. Maybe some Rust syntax-sugar going on pub fn test_thread_variable_with_scope(){ let age = 42; let person01 = Person {age, name: String::from("Trevor")}; // notice move is necessary to give the closure access to the parent variable let my_closure = || { println!("Closure vars:"); println!("age is {age}"); println!("name is {}", &person01.name); // &person01 is not necessary? }; println!("age is {age}"); // runs OK println!("name is {}", person01.name); std::thread::scope(|scope: &Scope| { scope.spawn(my_closure); }); println!("Thread is done"); println!("age is {age}");// runs OK println!("name is {}", person01.name); println!("finished main!") } Output: age is 42 name is Trevor Closure vars: age is 42 name is Trevor Thread is done age is 42 name is Trevor finished main!
Далее
Share Rust Thread Data With Mutexes 🦀 Rust Tutorial
35:36
Rust Threading Basics 🦀 Rust Tutorial
28:26
Просмотров 7 тыс.
Parse Rust CLI Args With Clap 🦀 Rust Tutorial
54:03
Popular Rust Iterator Methods 🦀
54:54
Просмотров 7 тыс.
Rust Lifetimes
26:52
Просмотров 51 тыс.
Rust HashMap and HashSet Collection Types 🦀
36:29
Просмотров 4,6 тыс.
Rust: Iterators
20:07
Просмотров 15 тыс.