Marker Traits

How does Rust know to forbid shared access across threads? The answer is in two traits:

  • Send:如果跨线程边界移动 T 是安全的,则类型 TSend
  • Sync:如果跨线程边界移动 &T 是安全的,则类型 TSync

Send and Sync are unsafe traits. The compiler will automatically derive them for your types as long as they only contain Send and Sync types. You can also implement them manually when you know it is valid.

  • 不妨将这些特征视为类型包含某些线程安全属性的标记。
  • 它们可以在泛型约束中作为常规特征使用。