Allowing Transference of Ownership Between Threads with Send
The Send
marker trait indicates that ownership of values of the type implementing Send
can be transferred between threads. Almost every Rust type is Send
, but there are some exceptions, including Rc<T>
: this cannot be Send
because if you cloned an Rc<T>
value and tried to transfer ownership of the clone to another thread, both threads might update the reference count at the same time. For this reason, Rc<T>
is implemented for use in single-threaded situations where you don't want to pay the thread-safe performance penalty.
Therefore, Rust's type system and trait bounds ensure that you can never accidentally send an Rc<T>
value across threads unsafely. When we tried to do this in Listing 16-14, we got the error the trait
Sendis not implemented for
Rc<Mutex<i32>
{=html}>`. When we switched to
Arc<T>
{=html}, which is
Send`, the code compiled.
Any type composed entirely of Send
types is automatically marked as Send
as well. Almost all primitive types are Send
, aside from raw pointers, which we'll discuss in Chapter 19.