Rust Scoped Threads 🦀 Rust Tutorial

Sdílet
Vložit
  • čas přidán 11. 09. 2024
  • 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

Komentáře • 15

  • @TrevorSullivan
    @TrevorSullivan  Před rokem +2

    Check out the FULL Rust playlist 🦀🦀🦀 czcams.com/play/PLDbRgZ0OOEpUkWDGqp91ODn0dk7LPBAUL.html

  • @shahidyousuf9464
    @shahidyousuf9464 Před 4 měsíci +3

    Your channel is definitely underrated!!! Deserves 100k subscriptions by now and more.

  • @FourTwentyMagic
    @FourTwentyMagic Před rokem +5

    Pretty sure structs are also stack allocated, Altho the first_name String would be heap-allocated

    • @davidjdoherty
      @davidjdoherty Před 2 měsíci

      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 Před 29 dny

      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

  • @AhmedFalih-kj3tt
    @AhmedFalih-kj3tt Před rokem +1

    thank you man for these videos, they are really helping me out ❤

  • @ians.2860
    @ians.2860 Před 5 měsíci

    This video was really helpful, thank you!

  • @peterhayman
    @peterhayman Před 6 měsíci

    great quality tutorials, thank you!

  • @fsaldan1
    @fsaldan1 Před 9 měsíci

    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  Před 9 měsíci

      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.

  • @ERAYKAAN850
    @ERAYKAAN850 Před rokem +3

    didn't watch it but nice video anyways (:

  • @AhmedFalih-kj3tt
    @AhmedFalih-kj3tt Před rokem +2

    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  Před rokem +6

      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.

  • @pooyannajafi
    @pooyannajafi Před 7 měsíci

    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!