Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.42 KB | None | 0 0
  1. // note
  2. // self.fishes => Vec<Box<fish::Fish>>
  3.  
  4. for f in self.fishes.iter() {
  5.             // if the fish is carnivorous
  6.             if let Some(fish) = f.as_ref().is_carnivorous() {
  7.  
  8.                 // looking for a meal
  9.                 // filtering out the fish that is eating
  10.                 let mut targets = self.fishes
  11.                     .iter()
  12.                     .filter(|&x| !same_object(f, x))
  13.                     .collect::<Vec<_>>();
  14.                 // is my filtering done right ? I'm not sure about the collect part ...
  15.  
  16.                 // picking a target at random
  17.                 let r = thread_rng().choose_mut(&mut targets);
  18.                 match r {
  19.                     // let's eat this target
  20.                     Some(&mut target_box) => {
  21.  
  22.                         fish.eat(target_box.as_mut());   //// issue here
  23.                         // eat(&self, &mut fish::Fish)
  24.                         // error : cannot borrow as mutable (on target_box in the call)
  25.                         // but my Box is mutable thanks to choose_mut, isn't it ?
  26.  
  27.                         if !target_box.as_ref().is_alive() {
  28.                             println!("dead fish");
  29.                         }
  30.                     }
  31.                     // No target found, no meal this turn ...
  32.                     None => println!("Cannot find a fish to eat")
  33.                 }
  34.             }
  35.             // else the fish is herbivorous
  36.             else {
  37.  
  38.             }
  39.  
  40.            
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement