Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Car {
- model: String
- }
- struct Customer<'a> {
- car: Option<&'a Car>
- }
- impl <'a> Customer<'a> {
- fn new() -> Customer<'a> {
- Customer {
- car: None
- }
- }
- fn buy_car(&mut self, c: &'a Car) {
- self.car = Some(c);
- }
- fn sell_car(&mut self) {
- self.car = None;
- }
- fn trade_with(&mut self, other: &mut Customer<'a>) {
- let tmp = other.car;
- other.car = self.car;
- self.car = tmp;
- }
- }
- fn main() {
- let mut fabia = &Car{ model: "Skoda Fabia".to_string() };
- let logan = Car{ model: "Renault Logan".to_string() };
- let mut bob = Customer::new();
- let mut alice = Customer::new();
- let mut artem = Customer::new();
- bob.buy_car(&mut fabia);
- alice.buy_car(&logan);
- artem.buy_car(&fabia);
- // bob.trade_with(&mut alice);
- //println!("{}", tempcar.model);
- // println!("{}", alice.car);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement