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

Let's go back to the game project. We'll add operations for the entities to move around the map, with all sorts of expected and unexpected error handling that might happen. Here are the data structures that we ended up with in Chapter 3, Unit Testing and Benchmarking:

#[derive(PartialEq, Debug)] enum TerrainGround {     Soil,     Stone } #[derive(PartialEq, Debug)] enum TerrainBlock {     Tree,     Soil,     Stone } #[derive(PartialEq, Debug)] enum Being {     Orc,     Human } struct Square {     ground: TerrainGround,     block: Option<TerrainBlock>,     beings: Option<Being> } struct Grid {     size: (usize, usize),     squares: Vec<Square> } 

So, we'll want to make it possible to move Being in any direction on Grid with the following cases being errors:

  • There ...

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