SHARE
TWEET

Untitled

a guest Aug 25th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use core::ops::Index;
  2.  
  3. struct Container {
  4.     item: u32,
  5. }
  6.  
  7. impl Container {
  8.     pub fn new() -> Container {
  9.         Container {item: 12345}
  10.     }
  11. }
  12.  
  13.  
  14. fn main() {
  15.     let con = Container::new();
  16.    
  17.     println!("{:?}", con["hej"]);
  18.     println!("{:?}", con[100]);
  19. }
  20.  
  21.  
  22.  
  23. impl Index<&str> for Container {
  24.     type Output = u32;
  25.    
  26.     fn index(&self, idx: &str) -> &u32 {
  27.         if idx == "hej" {
  28.             &10
  29.         } else {
  30.             &20
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36.  
  37. impl Index<u64> for Container {
  38.     type Output = u32;
  39.    
  40.     fn index(&self, idx: u64) -> &u32 {
  41.         &self.item
  42.     }
  43. }
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