E0594
rust
type_error
ai_generated
true
error[E0594]: cannot assign to data in an `Arc`
ID: rust/e0594-cannot-assign-to-data-in-an-arc
90%Fix Rate
88%Confidence
1Evidence
2023-01-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.65.0 | active | — | — | — |
| 1.68.0 | active | — | — | — |
| 1.72.0 | active | — | — | — |
Root Cause
Attempting to mutate data behind an `Arc<T>` without interior mutability (e.g., `Mutex`, `RwLock`, or `Cell`).
generic中文
尝试在没有内部可变性(例如 `Mutex`、`RwLock` 或 `Cell`)的情况下修改 `Arc<T>` 背后的数据。
Official Documentation
https://doc.rust-lang.org/error_codes/E0594.htmlWorkarounds
-
92% success Use `Arc<Mutex<T>>` and lock before mutation: `*arc.lock().unwrap() = new_value;`
Use `Arc<Mutex<T>>` and lock before mutation: `*arc.lock().unwrap() = new_value;`
-
95% success Use `Arc<AtomicU32>` for simple numeric types and `store` method.
Use `Arc<AtomicU32>` for simple numeric types and `store` method.
中文步骤
使用 `Arc<Mutex<T>>` 并在修改前加锁:`*arc.lock().unwrap() = new_value;`
对于简单的数值类型使用 `Arc<AtomicU32>` 和 `store` 方法。
Dead Ends
Common approaches that don't work:
-
Using `unsafe` to cast `Arc<T>` to a mutable reference via raw pointer
95% fail
Undefined behavior due to data races; violates Rust's safety guarantees.
-
Wrapping the entire `Arc` in a `RefCell` without thread safety
70% fail
`RefCell` is not `Sync`, so the `Arc<RefCell<T>>` cannot be shared across threads.