SHARE
TWEET

Untitled

a guest Oct 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top