Armanriazi•óxido•hilo•spawin•mover•captura de

Ejemplos de código

0
0

armanriazi•óxido•hilo•spawin•mover•captura de

{
  The move closure is often used alongside thread::spawn because it allows you to use data from one thread in another thread.
}
{
we’re not using any data from the main thread in the spawned thread’s code. To use data from the main thread in the spawned thread, the spawned thread’s closure must capture the values it needs.
}
{
The move keyword overrides Rust’s conservative default of borrowing; it doesn’t let us violate the ownership rules.
}
When the spawned thread wants to access variables that are defined in the parent’s scope, called a capture, Rust often complains that captures must be moved into the closure. To indicate that you want to move ownership, anonymous functions take a move keyword:
use std::{thread, time};
thread::spawn(move || {
    // ...
});

Why is move required? Closures spawned in subthreads can potentially outlive their calling scope. 
As Rust will always ensure that accessing the data is valid,
it requires ownership to move to the closure itself. Here are some guidelines for using captures while you gain an understanding of how these work:
To reduce friction at compile time, implement Copy.
Values originating in outer scopes may need to have a static lifetime.
Spawned subthreads can outlive their parents. That implies that ownership should pass to the subthread with move.

Páginas relacionadas

Páginas de ejemplo relacionadas

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................

Popular en esta categoría

Páginas populares con ejemplos en la categoría