Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::any::Any;
- use std::boxed::Box;
- use std::ops::Index;
- #[derive(Debug)]
- pub struct Test {
- a: u64,
- b: String
- }
- impl Index<&str> for Test {
- type Output = Box<dyn Any>;
- fn index(&self, val: &str) -> &Self::Output {
- match val{
- "a" => &Box::new(self.a),
- "b" => &Box::new(self.b),
- _ => unreachable!()
- }
- }
- }
- fn main() {
- let x = Test { a: 1, b: String::from("2")};
- println!("{:?}", x["b"]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement