我如何 return 对 HashMap 的键进行迭代以克隆键?

How may I return an iterator over the keys of a HashMap that clones the keys?

我正在定义一个名为 ShyAssociation 的特征,它 returns 是关联结构键的惰性迭代器。迭代器应该借用不可变的键或克隆它们。键总是 &'static str. 我只需要一个 returns &'static str.

的迭代器

这是我尝试 keys 迭代器方法的特征:

use std::collections::{hash_map::Keys, HashMap};

#[derive(Clone, PartialEq, Debug)]
pub enum ShyValue {
    Boolean(bool),
    Integer(i64),
    Rational(f64),
    String(String),
    Error(String),
}

pub trait ShyAssociation {
    fn keys(&self) -> Keys<&'static str, ShyValue>;
}

impl ShyAssociation for HashMap<&'static str, ShyValue> {
    fn keys(&self) -> Keys<&'static str, ShyValue> {
        self.keys().cloned()
    }
}

它不会编译。

error[E0308]: mismatched types
  --> src/lib.rs:18:9
   |
18 |         self.keys().cloned()
   |         ^^^^^^^^^^^^^^^^^^^^ expected struct `std::collections::hash_map::Keys`, found struct `std::iter::Cloned`
   |
   = note: expected type `std::collections::hash_map::Keys<'_, &'static str, _>`
              found type `std::iter::Cloned<std::collections::hash_map::Keys<'_, &str, _>>`

更新:

我正在尝试对 Alexander Huszagh 的回答进行变体,但出现语法错误:

use std::collections::{hash_map::Keys, HashMap};

#[derive(Clone, PartialEq, Debug)]
pub enum ShyValue {
    Boolean(bool),
    Integer(i64),
    Rational(f64),
    String(String),
    Error(String),
}

pub trait ShyAssociation<'a> {
    fn keys(&self) -> Box<Iterator<Item=&'static str> + 'a>;
}

impl<'a> ShyAssociation<'a> for HashMap<&'static str, ShyValue> {
    fn keys(&self) -> Box<Iterator<Item=&'static str> + 'a> {
        Box::new<(Iterator<Item=&'static str> + 'a)>(self.keys().cloned())
    }
}

错误消息在键的迭代器项定义中的 "str" 上:

expected `:`, found `str`

expected `:`rustc
shy_association.rs(59, 42): expected `:`

问题是您手动指定的类型与 return 类型不匹配。 iter::Cloned<Keys<_, _>>Keys<_, _> 不同。一个简单的解决方法是将 return 类型更改为 iter::Cloned<Keys<&'static str, ShyValue>>.

pub trait ShyAssociation {
    fn keys(&self) -> iter::Cloned<Keys<&'static str, ShyValue>>;
}

impl ShyAssociation for HashMap<&'static str, ShyValue> {
    fn keys(&self) -> iter::Cloned<Keys<&'static str, ShyValue>> {
        self.keys().cloned()
    }
}

如果想要 return 一个实现特征的类型(在上面的例子中不起作用,因为这只对非特征函数和方法有效),你也可以这样做:

pub fn keys<'a>(hash_map: &'a HashMap<&'static str, ShyValue>) -> impl Iterator<Item = &'a str> {
    hash_map.keys().cloned()
}

如果你想使用 Box<dyn Iterator> 以便你可以在特征方法中使用它,你可以这样做:

pub trait ShyAssociation {
    fn keys<'a>(&'a self) -> Box<(dyn Iterator<Item = &'static str> + 'a)>;
}

impl ShyAssociation for HashMap<&'static str, ShyValue> {
    fn keys<'a>(&'a self) -> Box<(dyn Iterator<Item = &'static str> + 'a)> {
        Box::new(self.keys().cloned())
    }
}

'a 生命周期是必要的,以便将迭代器的生命周期限制为 HashMap