Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(PartialEq)]
- struct Actor {
- hp: i32,
- attacking: bool,
- }
- impl Actor {
- pub fn new() -> Actor {
- Actor {
- hp: 10,
- attacking: false,
- }
- }
- pub fn damage(&mut self) {
- self.hp -= 1;
- }
- }
- fn main() {
- let mut actors = vec![Actor::new(), Actor::new()];
- actors[1].attacking = true;
- for actor in actors.iter_mut() {
- //if an actor is attacking, damage all the other actors
- if actor.attacking {
- //doesnt compile due to two mut borrows
- for actor2 in actors.iter_mut() {
- if actor2 != actor {
- actor2.damage();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement