E0277
rust
type_error
ai_generated
true
error[E0277]: `Rc<dyn Trait>` cannot be sent between threads safely
ID: rust/e0277-trait-bound-send-not-satisfied-for-rc
90%Fix Rate
85%Confidence
1Evidence
2024-02-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Rust 1.75.0 | active | — | — | — |
| Rust 1.80.0 | active | — | — | — |
| Rust nightly 2024-01-01 | active | — | — | — |
Root Cause
Rc is not Send because it uses reference counting without atomic operations, making it unsafe to share across threads.
generic中文
Rc 使用非原子引用计数,无法在线程间安全共享,因此未实现 Send trait。
Official Documentation
https://doc.rust-lang.org/stable/error_codes/E0277.htmlWorkarounds
-
95% success Replace `Rc<dyn Trait>` with `Arc<dyn Trait>` which uses atomic reference counting and implements Send
Replace `Rc<dyn Trait>` with `Arc<dyn Trait>` which uses atomic reference counting and implements Send
-
85% success Use `std::sync::Mutex<Box<dyn Trait>>` if you need mutable access from multiple threads
Use `std::sync::Mutex<Box<dyn Trait>>` if you need mutable access from multiple threads
中文步骤
Replace `Rc<dyn Trait>` with `Arc<dyn Trait>` which uses atomic reference counting and implements Send
Use `std::sync::Mutex<Box<dyn Trait>>` if you need mutable access from multiple threads
Dead Ends
Common approaches that don't work:
-
Wrap Rc in a Mutex: `Mutex<Rc<dyn Trait>>`
90% fail
Mutex only provides interior mutability, not Send. Rc still prevents Send because the reference count isn't atomic.
-
Add `unsafe impl Send for MyType` manually
95% fail
Unsafe impl can cause data races and undefined behavior if Rc is actually used across threads; the compiler warning is legitimate.