Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use core::ops::Index;
- struct Container {
- item: u32,
- }
- impl Container {
- pub fn new() -> Container {
- Container {item: 0}
- }
- }
- fn main() {
- let con = Container::new();
- println!("{:?}", con["hej"]);
- println!("{:?}", con[100]);
- }
- impl Index<&str> for Container {
- type Output = u32;
- fn index(&self, idx: &str) -> &u32 {
- if idx == "hej" {
- &10
- } else {
- &20
- }
- }
- }
- impl Index<u64> for Container {
- type Output = u32;
- fn index(&self, idx: u64) -> &u32 {
- &1000
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement