E0594
rust
type_error
ai_generated
true
错误[E0594]:无法赋值给 `Arc` 中的数据
error[E0594]: cannot assign to data in an `Arc`
ID: rust/e0594-cannot-assign-to-data-in-an-arc
90%修复率
88%置信度
1证据数
2023-01-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.65.0 | active | — | — | — |
| 1.68.0 | active | — | — | — |
| 1.72.0 | active | — | — | — |
根因分析
尝试在没有内部可变性(例如 `Mutex`、`RwLock` 或 `Cell`)的情况下修改 `Arc<T>` 背后的数据。
English
Attempting to mutate data behind an `Arc<T>` without interior mutability (e.g., `Mutex`, `RwLock`, or `Cell`).
官方文档
https://doc.rust-lang.org/error_codes/E0594.html解决方案
-
使用 `Arc<Mutex<T>>` 并在修改前加锁:`*arc.lock().unwrap() = new_value;`
-
对于简单的数值类型使用 `Arc<AtomicU32>` 和 `store` 方法。
无效尝试
常见但无效的做法:
-
Using `unsafe` to cast `Arc<T>` to a mutable reference via raw pointer
95% 失败
Undefined behavior due to data races; violates Rust's safety guarantees.
-
Wrapping the entire `Arc` in a `RefCell` without thread safety
70% 失败
`RefCell` is not `Sync`, so the `Arc<RefCell<T>>` cannot be shared across threads.