gpt4 book ai didi

hashmap - 以忽略 HashMap 值的通用方式公开 HashMap

转载 作者:行者123 更新时间:2023-11-29 08:15:39 26 4
gpt4 key购买 nike

我有不同的结构,它们都包含一个 HashMapString作为键,但具有不同的值类型。例如,一个结构有一个类型为 HashMap<String, String> 的成员, 另一个将有一个 HashMap<String, u8> 类型的成员, 等等。

我想定义一个可以访问这些 HashMap 的方法成员并对他们执行不涉及值的通用操作。例如,我想计算键的数量、删除键、检查键是否存在等。我不确定如何实现此行为。

到目前为止,我想到的最好方法是定义一个特征,该特征具有公开 HashMap 的方法。并让每个结构实现它。但是我不知道如何以“忽略”值类型的方式编写此特征和方法。我尝试使用通配符 ( _ ) 但它不起作用。我该如何实现?

这是我的代码(无法编译):

use std::collections::HashMap;

pub trait HashMapContainer {
fn get_hash_map(&self) -> HashMap<String, _>;
}

struct HashMapContainerImpl1 {
map: HashMap<String, String>,
}

impl HashMapContainerImpl1 {
pub fn new() -> HashMapContainerImpl1 {
HashMapContainerImpl1 {
map: HashMap::new(),
}
}

fn internal_logic_on_map(&mut self) {
//....
}
}

impl HashMapContainer for HashMapContainerImpl1 {
fn get_hash_map(&self) -> HashMap<String, _> {
self.map
}
}

struct HashMapContainerImpl2 {
map: HashMap<String, u8>,
}

impl HashMapContainerImpl2 {
pub fn new() -> HashMapContainerImpl2 {
HashMapContainerImpl2 {
map: HashMap::new(),
}
}

fn internal_logic_on_map(&mut self) {
//....
}
}

impl HashMapContainer for HashMapContainerImpl2 {
fn get_hash_map(&self) -> HashMap<String, _> {
self.map
}
}

fn do_generic_actions_on_map(hm_container: &HashMapContainer) {
println!("key count: {}", hm_container.get_hash_map().len());
println!(
"key esists? {}",
hm_container.get_hash_map().get("key1").is_some()
);
hm_container.get_hash_map().remove("key2");
}

fn main() {
let cont1 = HashMapContainerImpl1::new();
let cont2 = HashMapContainerImpl2::new();
do_generic_actions_on_map(cont1);
do_generic_actions_on_map(cont2);
}

最佳答案

关联类型

下面使用泛型的代码是正确的,但经过思考,我认为在这里使用关联类型可能更合适。该特征应如下所示:

pub trait HashMapContainer {
type Value;
fn get_hash_map(&self) -> &HashMap<String, Self::Value>;
fn get_hash_map_mut(&mut self) -> &mut HashMap<String, Self::Value>;
}

不同之处在于,您现在只能为一个结构实现一次特征,而不能多次实现,这在这种情况下更正确

实现与泛型类型参数大致相同。

impl HashMapContainer for HashMapContainerImpl1 {
type Value = String;
fn get_hash_map(&self) -> &HashMap<String, Self::Value> {
&self.map
}
fn get_hash_map_mut(&mut self) -> &mut HashMap<String, Self::Value> {
&mut self.map
}
}

impl HashMapContainer for HashMapContainerImpl2 {
type Value = u8;
fn get_hash_map(&self) -> &HashMap<String, Self::Value> {
&self.map
}
fn get_hash_map_mut(&mut self) -> &mut HashMap<String, Self::Value> {
&mut self.map
}
}

( Playground )

你也可以看看When is it appropriate to use an associated type versus a generic type?这很好地解释了这两者之间的区别。

使用泛型

这可以通过在 HashMapContainer 特征中引入泛型来解决。

pub trait HashMapContainer<T> {
fn get_hash_map(&self) -> &HashMap<String, T>;
fn get_hash_map_mut(&mut self) -> &mut HashMap<String, T>;
}

我更改了签名以返回对 HashMap 的引用。没有,例如,可以做到这一点通过使用 clone 或将 self 作为值,而不是作为引用。我还介绍了一个 _mut 版本。

实现是直接的前言:

impl HashMapContainer<String> for HashMapContainerImpl1 {
fn get_hash_map(&self) -> &HashMap<String, String> {
&self.map
}
fn get_hash_map_mut(&mut self) -> &mut HashMap<String, String> {
&mut self.map
}
}

impl HashMapContainer<u8> for HashMapContainerImpl2 {
fn get_hash_map(&self) -> &HashMap<String, u8> {
&self.map
}
fn get_hash_map_mut(&mut self) -> &mut HashMap<String, u8> {
&mut self.map
}
}

关于hashmap - 以忽略 HashMap 值的通用方式公开 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54231775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com