Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- trait Trait {
- fn metodo(&self) -> usize {
- 0
- }
- fn altro_metodo() -> usize;
- }
- struct Foo;
- impl Trait for Foo {
- fn altro_metodo() -> usize {
- 1
- }
- }
- fn main() -> Result<(), String> {
- let mut dictionary: HashMap<String, Box<Trait>> = HashMap::new();
- dictionary.insert(String::from("bar"), Box::new(Foo));
- dictionary.get("bar")
- .ok_or_else(|| String::from("\"bar\" not found"))
- .and_then(|o| {
- println!("{}", o.metodo());
- Ok(())
- })
- }
Add Comment
Please, Sign In to add comment