# 错误[E0277]：未满足特征约束 `RefCell<i32>: std::marker::Sync`

- **ID:** `rust/e0277-the-trait-bound-stdmarkersync-is-not-satisfied`
- **领域:** rust
- **类别:** type_error
- **错误码:** `E0277`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

RefCell 提供了内部可变性，但不是 Sync 的，因此无法通过 Arc 或静态变量在线程间共享。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| rustc 1.68.0 | active | — | — |
| rustc 1.70.0 | active | — | — |
| rustc 1.75.0 | active | — | — |

## 解决方案

1. ```
   将 RefCell 替换为 Mutex：`let shared = Arc::new(Mutex::new(42));` 然后使用 `shared.lock().unwrap()` 加锁。Mutex 是 Sync 的，可以跨线程安全地提供内部可变性。
   ```
2. ```
   对于简单整数，使用原子类型：`use std::sync::atomic::{AtomicI32, Ordering}; let shared = Arc::new(AtomicI32::new(42)); shared.store(100, Ordering::SeqCst);`
   ```
3. ```
   如果只需要单线程访问，使用 `Rc<RefCell<i32>>` 代替 `Arc<RefCell<i32>>`。
   ```

## 无效尝试

- **** — Sync is a trait for safe shared access across threads, not related to cloning. Deriving Clone only enables cloning, not thread safety. (95% 失败率)
- **** — Arc provides shared ownership but does not make the inner type Sync. Mutex already provides Sync, so wrapping RefCell inside Mutex is unnecessary and the outer Arc doesn't help. (80% 失败率)
- **** — Marking RefCell as Sync is unsound because it allows data races. The compiler correctly prevents this. (70% 失败率)
