E0599 rust type_error ai_generated true

错误[E0599]:在当前作用域中,结构体 `std::collections::HashMap<String, i32>` 上没有找到名为 `iter` 的方法

error[E0599]: no method named `iter` found for struct `std::collections::HashMap<String, i32>` in the current scope

ID: rust/e0599-no-method-named-iter-found

其他格式: JSON · Markdown 中文 · English
93%修复率
87%置信度
1证据数
2023-01-20首次发现

版本兼容性

版本状态引入弃用备注
rustc 1.58.0 active
rustc 1.64.0 active
rustc 1.71.0 active

根因分析

HashMap 没有返回元素迭代器的 `iter` 方法;应对引用使用 `iter()` 或对所有权使用 `into_iter()`。

English

HashMap does not have an `iter` method that returns an iterator over items; use `iter()` on references or `into_iter()` for owned iteration.

generic

官方文档

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

解决方案

  1. 对引用使用 `map.iter()`:`for (key, value) in &map { ... }` 或 `let iter = map.iter();`
  2. 对所有权使用 `into_iter()`:`for (key, value) in map { ... }`
  3. 使用 `values()` 或 `keys()` 方法:`for value in map.values() { ... }`

无效尝试

常见但无效的做法:

  1. 90% 失败

    Iterator is automatically in scope. The issue is that HashMap doesn't have a method named `iter` without the trait bounds being satisfied.

  2. 70% 失败

    The method exists but is named `into_iter()` for owned HashMap. `iter()` exists on references like `&map`.

  3. 95% 失败

    Iterator is a trait, not a derive macro. You cannot derive Iterator for HashMap.