Reputation: 83
I have a function in golang -
func (s *Server) getEnforcer(handle int) (*casbin.Enforcer, error) {
if _, ok := s.enforcerMap[handle]; ok {
return s.enforcerMap[handle], nil
} else {
return nil, errors.New("enforcer not found")
}
}
I am trying to implement this in rust. I have written this -
impl Server {
fn getEnforcer(&mut self, handle: i32) -> Result<Enforcer, Box<dyn Error>> {
let e: Enforcer = self.enforcerMap[&handle];
// match ..
}
}
Can't figure out how to handle error.
Upvotes: 0
Views: 1622
Reputation: 133
Better yet, return an option because the only possible error is that the index is not present in the map,
so..
impl Server {
pub fn getEnforcer(&self,handle:i32)->Option<&Enforcer> {
self.enforcerMap.get(&handle)
}
pub fn getEnforcerMut(&mut self,handle:i32)->Option<&mut Enforcer> {
self.enforcerMap.get_mut(&handle)
}
}
however I highly suggest this link and this link
Upvotes: 3