Code shared from the Rust Playground
| type BarberId= usize; | |
| type ShopId = usize; | |
| struct Shop { | |
| barber: Option<BarberId>, | |
| } | |
| struct Barber { | |
| shop: Option<ShopId>, | |
| } | |
| struct MyDataStore { | |
| barbers: Vec<Barber>, | |
| shops: Vec<Shop> | |
| } | |
| impl MyDataStore { | |
| fn add_barber(&mut self, barber: Barber) -> BarberId { | |
| let id = self.barbers.len(); | |
| self.barbers.push(barber); | |
| id | |
| } | |
| fn add_shop(&mut self, shop: Shop) -> ShopId { | |
| let id = self.shops.len(); | |
| self.shops.push(shop); | |
| id | |
| } | |
| fn assign_barber_and_shop(&mut self, shop_id: ShopId, barber_id: BarberId) { | |
| self.barbers[barber_id].shop = Some(shop_id); | |
| self.shops[shop_id].barber = Some(barber_id); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment