Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. use std::any::Any;
  2. use std::boxed::Box;
  3. use std::ops::Index;
  4.  
  5. #[derive(Debug)]
  6. pub struct Test {
  7. a: u64,
  8. b: String
  9. }
  10.  
  11. impl Index<&str> for Test {
  12. type Output = Box<dyn Any>;
  13.  
  14. fn index(&self, val: &str) -> &Self::Output {
  15. match val{
  16. "a" => &Box::new(self.a),
  17. "b" => &Box::new(self.b),
  18. _ => unreachable!()
  19. }
  20. }
  21. }
  22.  
  23. fn main() {
  24. let x = Test { a: 1, b: String::from("2")};
  25. println!("{:?}", x["b"]);
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement