附录E1. Rust 编译错误代码索引本索引收录 Rust 开发中最常遇到的 120 个编译错误按出现频率从高到低排列。每个错误的详细说明、示例代码和解决方案请参阅对应的附录文档E2-E7。如何查看 Rust 编译错误Rust 的每个编译错误都有一个唯一的错误代码形如EXXXX我们可以通过以下三种方式查看某个错误的详细说明方式一官方错误索引网站Rust 官方维护了完整的错误索引页面涵盖所有已知的编译错误代码及其解释https://doc.rust-lang.org/error-index.html在页面中可以直接搜索错误代码如E0308即可看到官方的详细说明和示例。这是最权威、最完整的参考来源。方式二命令行工具rustc --explain在终端中使用rustc --explain命令可以直接在本地查看任意错误代码的解释无需联网# 查看 E0308 的详细解释rustc--explainE0308# 查看 E0382 的详细解释rustc--explainE0382该命令会输出错误的原因、常见场景和修复建议内容与官方错误索引网站一致。推荐在开发过程中遇到不熟悉的错误时立即使用此命令。方式三查看编译器源码中的错误定义对于想深入理解错误原理的读者可以直接查看 Rust 编译器源码中的错误定义。所有编译错误都定义在rust仓库的compiler/目录下https://github.com/rust-lang/rust/tree/master/compiler错误描述文件通常位于各编译阶段对应的diagnostics.rs或errors.rs中。例如类型检查相关的错误在compiler/rustc_hir_typeck/下借用检查相关的错误在compiler/rustc_borrowck/下。通过搜索错误代码如E0308可以快速定位到具体的诊断代码了解编译器是如何检测和报告这些错误的。建议日常开发中使用前两种方式即可快速解决问题。第三种方式适合对编译器原理感兴趣、或想为 Rust 编译器贡献诊断改进的读者。错误代码速查表序号错误代码描述分类详解文档1E0308类型不匹配mismatched types类型附录E22E0382使用已移动的值use of moved value所有权附录E23E0277Trait 约束未满足trait bound not satisfiedTrait附录E24E0425找不到值或函数cannot find value/function名称解析附录E25E0433未解析的路径failed to resolve: use of undeclared crate/module名称解析附录E26E0599找不到方法no method named X found方法附录E27E0502可变借用与不可变借用冲突mutable/immutable borrow conflict借用附录E28E0061函数参数数量不正确wrong number of arguments函数附录E29E0412找不到类型cannot find type名称解析附录E210E0369运算符不能应用于该类型operator cannot be applied类型附录E211E0282需要类型标注type annotations needed类型推断附录E212E0432未解析的导入unresolved import模块附录E213E0107泛型参数数量不正确wrong number of generic arguments泛型附录E214E0596不能可变借用不可变变量cannot borrow as mutable可变性附录E215E0499多个可变引用冲突multiple mutable borrows借用附录E216E0597值存活时间不够长value does not live long enough生命周期附录E217E0106缺少生命周期标注missing lifetime specifier生命周期附录E218E0381使用可能未初始化的变量possibly uninitialized variable初始化附录E219E0004模式匹配不完整non-exhaustive patterns模式匹配附录E220E0507不能从借用内容中移动cannot move out of borrowed content所有权附录E221E0373闭包可能超出借用数据的生命周期closure may outlive borrowed data闭包附录E322E0594不能修改不可变项cannot assign to immutable item可变性附录E323E0384不能二次赋值给不可变变量cannot assign twice to immutable variable可变性附录E324E0609类型没有此字段no field on type字段访问附录E325E0063结构体初始化缺少字段missing fields in struct结构体附录E326E0046未实现所有 Trait 方法not all trait items implementedTrait附录E327E0423期望值发现结构体变体expected value, found struct variant名称解析附录E328E0506不能赋值因为已被借用cannot assign because it is borrowed借用附录E329E0005可反驳模式用于不可反驳位置refutable pattern in local binding模式匹配附录E330E0119Trait 实现冲突conflicting implementations of traitTrait附录E331E0133使用 unsafe 操作但缺少 unsafe 块use of unsafe requires unsafe blockunsafe附录E332E0728await 在非 async 上下文中使用await outside async context异步附录E333E0451结构体字段是私有的field is private可见性附录E334E0603模块/结构体/函数是私有的item is private可见性附录E335E0271关联类型不匹配type mismatch resolving associated typeTrait附录E336E0621需要显式生命周期标注explicit lifetime required生命周期附录E337E0310类型可能活得不够长type may not live long enough生命周期附录E338E0658使用了不稳定特性use of unstable feature稳定性附录E339E0716临时值在被借用时被丢弃temporary value dropped while borrowed生命周期附录E340E0225只有自动 Trait 可以作为附加 Trait 对象non-auto trait in trait objectTrait附录E341E0283类型标注需要多个候选有歧义ambiguous type类型推断附录E442E0404期望 Trait发现其他项expected trait, found something elseTrait附录E443E0407方法不属于 Trait 定义method not a member of traitTrait附录E444E0428名称重复定义duplicate definition名称解析附录E445E0015常量上下文中调用非 const 函数non-const function in const context常量附录E446E0392未使用的类型参数unused type parameter泛型附录E447E0275求值 Trait 约束时溢出overflow evaluating requirementTrait附录E448E0614不能解引用该类型cannot dereference type解引用附录E449E0615尝试获取方法的值而非调用attempted to take value of method方法附录E450E0317if/else 分支类型不一致或缺少 elseif may be missing an else clause控制流附录E451E0368二元赋值运算不能应用binary assignment not applicable运算符附录E452E0408变量未在所有模式分支中绑定variable not bound in all patterns模式匹配附录E453E0252导入名称冲突import name conflict模块附录E454E0261使用了未声明的生命周期名称use of undeclared lifetime生命周期附录E455E0401内部项不能捕获外部动态环境变量can’t capture dynamic environment闭包附录E456E0515不能返回引用局部变量的值cannot return reference to local variable所有权附录E457E0562impl Trait 不允许在此位置使用impl Trait not allowed hereTrait附录E458E0525闭包不满足 Fn/FnMut/FnOnce 约束closure does not fulfill bound闭包附录E459E0529模式期望数组或切片expected array or slice模式匹配附录E460E0532期望模式发现函数或常量expected pattern, found function模式匹配附录E461E0559枚举变体没有此字段variant has no field枚举附录E562E0560结构体没有此字段struct has no field结构体附录E563E0574期望结构体/枚举/联合类型expected struct, enum, or union type类型附录E564E0583找不到模块对应的文件file not found for module模块附录E565E0277类型大小在编译期未知Sized bound not satisfied类型附录E566E0277类型不是 Sendtype is not Send并发附录E567E0277类型不是 Synctype is not Sync并发附录E568E0133解引用裸指针需要 unsafedereference of raw pointer requires unsafeunsafe附录E569E0493析构函数不能在常量中求值destructor cannot be evaluated at compile-time常量附录E570E0520固有方法与 Trait 方法同名冲突inherent method shadows trait method方法附录E571E0521借用的数据逃逸闭包/函数borrowed data escapes closure借用附录E572E0200Trait 的关联函数必须没有 self 参数或者有 self 参数trait fn signature mismatchTrait附录E573E0053方法签名与 Trait 声明不匹配method signature mismatchTrait附录E574E0054不能将类型转换为目标类型cannot cast type类型转换附录E575E0055解引用次数过多too many dereferences解引用附录E576E0057闭包参数数量不正确wrong number of closure arguments闭包附录E577E0060可变参数函数variadic function requires unsafe函数附录E578E0062字段被多次初始化field specified more than once结构体附录E579E0069return 语句的值与函数返回类型不匹配return type mismatch函数附录E580E0071使用结构体语法构造非结构体类型struct syntax on non-struct类型附录E581E0080常量求值错误constant evaluation error常量附录E682E0084不允许的枚举判别式类型unsupported enum discriminant type枚举附录E683E0091未使用的类型参数unused type parameter泛型附录E684E0093未声明的 intrinsic 函数undeclared intrinsic functionFFI附录E685E0116不能为外部类型定义固有 implcannot define inherent impl for foreign typeTrait附录E686E0117孤儿规则不能为外部类型实现外部 Traitorphan rule violationTrait附录E687E0120不能为 Drop 实现目标drop impl requires same typeDrop附录E688E0124结构体字段重名duplicate field name结构体附录E689E0128类型参数的默认值中引用了后续参数type parameter default references later param泛型附录E690E0131main 函数签名不正确invalid main function signature函数附录E691E0152重复的 lang itemduplicate lang item编译器附录E692E0161不能移动 unsized 类型的值cannot move a value of unsized type类型附录E693E0162不可反驳的 if-let 模式irrefutable if-let pattern模式匹配附录E694E0164期望带有字段的结构体变体expected struct variant, found tuple variant模式匹配附录E695E0184Copy 和 Drop 不能同时实现Copy and Drop are exclusiveTrait附录E696E0185方法的 self 参数与 Trait 声明不匹配method self parameter mismatchTrait附录E697E0191关联类型未指定missing associated typeTrait附录E698E0195生命周期参数与 Trait 声明不匹配lifetime parameters mismatch生命周期附录E699E0197unsafe Trait 的 impl 缺少 unsafe 标记inherent impl marked unsafeunsafe附录E6100E0198非 unsafe Trait 的 impl 标记了 unsafenegative impl marked unsafeunsafe附录E6101E0204不能为包含非 Copy 字段的类型实现 CopyCopy for type with non-Copy fieldTrait附录E7102E0207类型/生命周期参数未被约束unconstrained type/lifetime parameter泛型附录E7103E0210Trait 实现违反孤儿规则的类型参数约束type parameter violates orphan rulesTrait附录E7104E0214带泛型参数的括号表示法需要 Fn Traitparenthesized parameters require Fn trait语法附录E7105E0220关联类型未找到associated type not foundTrait附录E7106E0229关联类型绑定不允许在此位置associated type bindings not allowed hereTrait附录E7107E0243泛型参数过少incorrect number of type parameters: too few泛型附录E7108E0244泛型参数过多incorrect number of type parameters: too many泛型附录E7109E0255导入名称与当前模块中已有名称冲突imported name conflicts with existing模块附录E7110E0259extern crate 名称与已有名称冲突extern crate name conflict模块附录E7111E0267break 在循环或块之外使用break outside of loop控制流附录E7112E0268continue 在循环之外使用continue outside of loop控制流附录E7113E0276impl 对 Trait 方法施加了更严格的约束impl imposes stricter requirementsTrait附录E7114E0277类型没有实现 Display/DebugDisplay/Debug not implemented格式化附录E7115E0303在绑定之后不允许子模式pattern bindings not allowed after模式匹配附录E7116E0326关联常量的类型不匹配associated const type mismatchTrait附录E7117E0364私有项不能在公开接口中使用private item in public interface可见性附录E7118E0370枚举判别式溢出enum discriminant overflowed枚举附录E7119E0403函数参数名重复duplicate parameter name函数附录E7120E0424在非方法上下文中使用 selfself used outside of impl语法附录E7详解文档目录文档覆盖错误说明附录E2#1-#20E0308 ~ E0507附录E3#21-#40E0373 ~ E0225附录E4#41-#60E0283 ~ E0532附录E5#61-#80E0559 ~ E0071附录E6#81-#100E0080 ~ E0198附录E7#101-#120E0204 ~ E0424