E0277
rust
type_error
ai_generated
true
错误[E0277]:`Rc<RefCell<dyn Trait>>` 无法在线程间安全发送
error[E0277]: `Rc<RefCell<dyn Trait>>` cannot be sent between threads safely
ID: rust/e0277-send-not-satisfied-for-rc
85%修复率
85%置信度
1证据数
2023-06-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| rustc 1.68 | active | — | — | — |
| rustc 1.75 | active | — | — | — |
| rustc 1.82 | active | — | — | — |
根因分析
Rc 使用非原子引用计数,跨线程使用会导致数据竞争,因此未实现 Send。
English
Rc is not Send because it uses reference counting without atomic operations; cross-thread use causes data races.
官方文档
https://doc.rust-lang.org/stable/std/marker/trait.Send.html解决方案
-
将 Rc 替换为 Arc(原子引用计数),并使用 Arc<Mutex<T>> 或 Arc<RwLock<T>> 实现内部可变性。
-
使用 `std::sync::Mutex<RefCell<dyn Trait>>` 或 `parking_lot::Mutex` 如果必须保留 Rc,但推荐使用 Arc。
无效尝试
常见但无效的做法:
-
70% 失败
Mutex<Arc<RefCell<...>>> adds overhead and doesn't solve the fundamental Rc issue; use Arc instead.
-
90% 失败
Deriving Clone doesn't change thread safety; the compiler still requires Send on the inner type.