O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise solutions

  1. Implement the error case where the Being tries to fall from the edge of the Grid.
  2. Implement the error case where the Being tries to move into a Square where there is already a Being.
  1. Implement the error case where the Being tries to move to a Terrain that is Stone.
  2. Implement the happy case where no errors happen and the Being successfully moves to the new Square.

Solution: You may have noticed that 1-3 can be implemented in a straight-forward manner, but at 4, when we need to mutate the squares, we crash into some obstacles. We solved this by cloning the whole squares of the grid, which is obviously a tad inefficient, but works.

Here are the filled move_being_in_coord method and related unit tests:

 fn move_being_in_coord(&mut ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required