# 错误[E0308]：类型不匹配——期望`i32`，但找到`u32`

- **ID:** `rust/e0308-mismatched-types-expected-i32-found-u32`
- **领域:** rust
- **类别:** type_error
- **错误码:** `E0308`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

由于Rust严格的类型系统不允许隐式转换，在期望一种整数类型的地方使用了另一种整数类型。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| rustc 1.60.0 | active | — | — |
| rustc 1.65.0 | active | — | — |
| rustc 1.70.0 | active | — | — |

## 解决方案

1. ```
   使用`From`或`TryFrom`特征进行显式转换，例如`i32::try_from(u32_val)`并处理Result。
   ```
2. ```
   如果值保证在范围内，使用`as`强制转换并添加注释说明安全性。
   ```

## 无效尝试

- **** — `as` cast can silently truncate or wrap, leading to data loss; it's not a clean conversion. (70% 失败率)
- **** — If the source value is inherently u32 (e.g., from an API), changing the variable type may cause other mismatches. (60% 失败率)
- **** — Unsafe coercion is dangerous and undefined behavior; it doesn't solve the type mismatch. (95% 失败率)
