【发布时间】:2021-09-21 07:20:22
【问题描述】:
pub trait Observer{
type DataType;
fn new(action: fn(&mut Self::DataType))->Self;
fn update(&self, data:&mut Self::DataType);
}
pub trait Subject{
type ObserverType: Observer;
fn new()-> Self;
fn add_observer(&mut self,observer:Rc<Self::ObserverType>);
fn remove_observer(&mut self,observer:&Rc<Self::ObserverType>);
fn data(&mut self)-> &<<Self as Subject>::ObserverType as Observer>::DataType;
fn notification(&mut self);
}
pub struct SubjectMgr{
}
impl SubjectMgr {
fn new(){
let mut map = HashMap::new();
map.insert("PlayerList",PlayerListSubject::new());
map.insert("MonsterList",MonsterListSubject::new());
}
}
尝试使用具有关联值的哈希映射作为成员。但是我不能有一个 hashmap 值类型吗?
文件结构如下: enter image description here
【问题讨论】:
标签: dictionary generics rust hashmap associate