Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // note
- // self.fishes => Vec<Box<fish::Fish>>
- for f in self.fishes.iter() {
- // if the fish is carnivorous
- if let Some(fish) = f.as_ref().is_carnivorous() {
- // looking for a meal
- // filtering out the fish that is eating
- let mut targets = self.fishes
- .iter()
- .filter(|&x| !same_object(f, x))
- .collect::<Vec<_>>();
- // is my filtering done right ? I'm not sure about the collect part ...
- // picking a target at random
- let r = thread_rng().choose_mut(&mut targets);
- match r {
- // let's eat this target
- Some(&mut target_box) => {
- fish.eat(target_box.as_mut()); //// issue here
- // eat(&self, &mut fish::Fish)
- // error : cannot borrow as mutable (on target_box in the call)
- // but my Box is mutable thanks to choose_mut, isn't it ?
- if !target_box.as_ref().is_alive() {
- println!("dead fish");
- }
- }
- // No target found, no meal this turn ...
- None => println!("Cannot find a fish to eat")
- }
- }
- // else the fish is herbivorous
- else {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement