Guest User

Untitled

a guest
Dec 11th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. use std::collections::HashMap;
  2.  
  3. trait Trait {
  4. fn metodo(&self) -> usize {
  5. 0
  6. }
  7.  
  8. fn altro_metodo() -> usize;
  9. }
  10.  
  11. struct Foo;
  12.  
  13. impl Trait for Foo {
  14. fn altro_metodo() -> usize {
  15. 1
  16. }
  17. }
  18.  
  19. fn main() -> Result<(), String> {
  20. let mut dictionary: HashMap<String, Box<Trait>> = HashMap::new();
  21. dictionary.insert(String::from("bar"), Box::new(Foo));
  22.  
  23. dictionary.get("bar")
  24. .ok_or_else(|| String::from("\"bar\" not found"))
  25. .and_then(|o| {
  26. println!("{}", o.metodo());
  27. Ok(())
  28. })
  29. }
Add Comment
Please, Sign In to add comment