Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #[derive(PartialEq)]
  2. struct Actor {
  3. hp: i32,
  4. attacking: bool,
  5. }
  6.  
  7. impl Actor {
  8. pub fn new() -> Actor {
  9. Actor {
  10. hp: 10,
  11. attacking: false,
  12. }
  13. }
  14.  
  15. pub fn damage(&mut self) {
  16. self.hp -= 1;
  17. }
  18. }
  19.  
  20. fn main() {
  21. let mut actors = vec![Actor::new(), Actor::new()];
  22. actors[1].attacking = true;
  23.  
  24. for actor in actors.iter_mut() {
  25. //if an actor is attacking, damage all the other actors
  26. if actor.attacking {
  27. //doesnt compile due to two mut borrows
  28. for actor2 in actors.iter_mut() {
  29. if actor2 != actor {
  30. actor2.damage();
  31. }
  32. }
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement