# 错误[E0080]：将 `bool` 转换为 `char` 是未定义行为

- **ID:** `rust/e0080-invalid-cast-from-bool-to-char`
- **领域:** rust
- **类别:** type_error
- **错误码:** `E0080`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

将 bool 转换为 char 是未定义行为，因为 bool 只有值 0 和 1，但 char 要求有效的 Unicode 标量值（0x0 到 0x10FFFF，不包括代理项）。

## 版本兼容性

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

## 解决方案

1. ```
   使用 match 进行显式转换：`let c = if b { '1' } else { '0' };`
   ```
2. ```
   使用 `char::from_digit(b as u32, 10).unwrap_or('0')` 获取数字字符。
   ```
3. ```
   先转换为 u8：`let c = (b as u8 + b'0') as char;` 用于 ASCII 数字。
   ```

## 无效尝试

- **** — Rust's as cast is not allowed for bool-to-char because it's UB. The compiler rejects it. (95% 失败率)
- **** — Transmute is unsafe and still UB. It might compile but is undefined behavior. (80% 失败率)
- **** — There is no From<bool> impl for char. This won't compile. (90% 失败率)
