Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  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: 0}
  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. &1000
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement