`T` cannot be shared between threads safely: the trait `Sync` is not implemented for `T` – Rust E0277_SYNC

E0277_SYNC

L'errore compare quando si tenta di condividere un riferimento immutabile &T tra più thread simultaneamente senza che il tipo implementi il trait 'Sync', tipicamente risolvibile avvolgendo il tipo in un Mutex o RwLock.

Cosa significa questo errore?

L'errore compare quando si tenta di condividere un riferimento immutabile &T tra più thread simultaneamente senza che il tipo implementi il trait 'Sync', tipicamente risolvibile avvolgendo il tipo in un Mutex o RwLock.

Messaggio originale

`T` cannot be shared between threads safely: the trait `Sync` is not implemented for `T`

Contesto e parole chiave

Alias

  • cannot be shared between threads safely Sync

Messaggi alternativi

  • trait Sync non implementato per condivisione tra thread

Prodotti

  • Rust Compiler (rustc) / Cargo / Tokio

Piattaforme

  • Linux
  • macOS
  • Windows

Parole chiave

  • Concorrenza e Async
  • errore-rust-cannot-be-shared-between-threads-sync
  • rust
  • rust.async.cannot-be-shared-between-threads-sync-not-implemented

Cause possibili

Causa tecnica per Errore Rust Concorrenza: `T` cannot be shared between threads safely (Sync Non Soddisfatto)

L'errore compare quando si tenta di condividere un riferimento immutabile &T tra più thread simultaneamente senza che il tipo implementi il trait 'Sync', tipicamente risolvibile avvolgendo il tipo in un Mutex o RwLock.

molto comune

Diagnosi

Verifica di compilazione e diagnostica per Errore Rust Concorrenza: `T` cannot be shared between threads safely (Sync Non Soddisfatto)

Eseguire il comando cargo check per visualizzare l'output dettagliato del compilatore con suggerimenti (explain).

Come leggere il risultato. Fornisce la spiegazione estesa dell'errore di compilazione o stack trace del panic.

cargo
cargo check --explain E0277_SYNC

Risoluzione

Risoluzione standard per Errore Rust Concorrenza: `T` cannot be shared between threads safely (Sync Non Soddisfatto)

Difficoltà: mediaRischio: basso

Applica le correzioni consigliate per conformarsi alle regole di ownership, tipi o runtime in Rust.

  1. Ispezionare la riga evidenziata dal compilatore rustc
  2. Riformulare il codice secondo le linee guida della gestione ownership e borrow
  3. Eseguire cargo check per confermare la risoluzione
cargo
cargo check

Domande frequenti

Cosa significa Errore Rust Concorrenza: `T` cannot be shared between threads safely (Sync Non Soddisfatto)?

L'errore compare quando si tenta di condividere un riferimento immutabile &T tra più thread simultaneamente senza che il tipo implementi il trait 'Sync', tipicamente risolvibile avvolgendo il tipo in un Mutex o RwLock.

Approfondimenti

Schede correlate