简体   繁体   English

我如何在克隆密钥的HashMap的密钥上返回迭代器?

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

I am defining a trait called ShyAssociation that returns a lazy iterator of the keys of an associative structure. 我正在定义一个称为ShyAssociation的特征,该特征返回关联结构键的惰性迭代器。 The iterator should either borrow the keys as immutable or clone them. 迭代器应借用不可变的密钥或将其克隆。 The keys are always &'static str. 键始终为&'static str. All I need is an iterator that returns &'static str . 我需要的是一个返回&'static str的迭代器。

Here is the trait with my attempt at the method for the keys iterator: 这是我尝试使用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()
    }
}

It won't compile. 它不会编译。

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, _>>`

UPDATE: 更新:

I am attempting a variation on Alexander Huszagh's answer, but have a syntax error: 我正在尝试对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())
    }
}

The error message is on the "str" in the Iterator Item definition for keys: 错误消息位于键的迭代器项定义中的“ str”上:

expected `:`, found `str`

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

The issue is that you're manually specifying a type that doesn't match the return type. 问题是您要手动指定与返回类型不匹配的类型。 iter::Cloned<Keys<_, _>> is not the same as Keys<_, _> . iter::Cloned<Keys<_, _>>Keys<_, _> A simple fix is to change your return type to iter::Cloned<Keys<&'static str, ShyValue>> . 一个简单的解决方法是将您的返回类型更改为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()
    }
}

If would like to return a type that implements a trait (which will not work in the above example, since this is only valid for non-trait functions and methods), you may also do: 如果要返回实现特征的类型(在上例中将不起作用,因为这仅对非特征函数和方法有效),您也可以执行以下操作:

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

If you would like to use Box<dyn Iterator> so you may use it in a trait method, you may do: 如果要使用Box<dyn Iterator>以便可以在trait方法中使用它,则可以执行以下操作:

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())
    }
}

The 'a lifetime is necessary in order to limit the lifetime of the iterator to that of the HashMap . 'a寿命是必要的,以便迭代器的寿命限制于所述的HashMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM