E0080 rust const-eval ai_generated true

错误[E0080]:常量求值错误:除以零

error[E0080]: constant evaluation error: division by zero

ID: rust/e0080-constant-evaluation-error-divide-by-zero

其他格式: JSON · Markdown 中文 · English
95%修复率
90%置信度
1证据数
2023-06-05首次发现

版本兼容性

版本状态引入弃用备注
1.63.0 active
1.66.0 active
1.70.0 active

根因分析

常量表达式在编译时执行了除以零或取模零的操作。

English

A constant expression performs division or modulo by zero at compile time.

generic

官方文档

https://doc.rust-lang.org/error_codes/E0080.html

解决方案

  1. 通过条件确保除数非零:`const DIV: u32 = if DENOM != 0 { NUM / DENOM } else { 0 };`
  2. 使用检查算术:`const RESULT: Option<u32> = NUM.checked_div(DENOM);`

无效尝试

常见但无效的做法:

  1. Using `unsafe` to bypass the check in const context 100% 失败

    `unsafe` is not allowed in const contexts; compiler rejects it.

  2. Changing the divisor to a variable to defer evaluation to runtime 70% 失败

    If the divisor is still zero at runtime, it causes a panic; compile-time check is bypassed but runtime error remains.