In Rust, an auto trait is a special kind of trait that is automatically implemented for types satisfying certain conditions. The most common examples are the Send and Sync traits:
- The
Sendtrait indicates that a value of a type can be safely moved between threads. - The
Synctrait indicates that a value of a type can be safely shared across multiple threads, meaning it is safe to access concurrently from multiple threads. These traits do not require explicit implementation; instead, they are automatically derived based on their constituent parts. If all components of a type areSend, then the type is automaticallySend. Similarly, if all components of a type areSync, then the type is automaticallySync. A key feature of auto traits is that they utilizenegative reasoning, meaning by default all types implement these traits unless explicitly opted out viaopt-outmechanism (for example, by marking a custom type as notSendor notSyncusing thestd::marker::PhantomDatatype). Overall, auto traits provide an efficient way to handle thread safety, allowing developers to focus more on logical implementation rather than the thread safety details of each type.