# 错误[E0308]：类型不匹配：期望 `usize`，得到 `f64`

- **ID:** `rust/e0308-expected-usize-found-float`
- **领域:** rust
- **类别:** type_error
- **错误码:** `E0308`
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

在需要无符号整数索引的位置（如数组索引或切片操作）使用了浮点数。

## 版本兼容性

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

## 解决方案

1. ```
   Use `as usize` to convert f64 to usize, ensuring the value is non-negative and within range: `let index = value as usize;`
   ```
2. ```
   Use `f64::round()` or `f64::floor()` before casting: `let index = value.round() as usize;`
   ```
3. ```
   If the value is from user input or computation, parse or compute it as `usize` directly: `let index: usize = some_usize_source;`
   ```

## 无效尝试

- **** — Truncation may cause logic errors; the compiler allows it but the behavior is undefined for negative or NaN values. (45% 失败率)
- **** — Rust requires explicit conversion; this approach is syntactically invalid. (100% 失败率)
- **** — If the value comes from a computation or input that yields f64, the type mismatch persists. (60% 失败率)
