Rust Scoped Threads 🦀 Rust Tutorial

Published 2023-09-04
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 Tutorial 🦀  

📖 Rust std::thread::scope docs ➡️ doc.rust-lang.org/std/thread/fn.scope.html

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
➡️ twitter.com/pcgeek86
➡️ youtube.com/trevorsullivan
➡️ facebook.com/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

All Comments (10)
  • @FourTwentyMagic
    Pretty sure structs are also stack allocated, Altho the first_name String would be heap-allocated
  • @AhmedFalih-kj3tt
    thank you man for these videos, they are really helping me out ❤
  • @ians.2860
    This video was really helpful, thank you!
  • @fsaldan1
    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?
  • @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. ♥
  • @pooyannajafi
    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!