Hands-on Rust: Unnecessary cast to usize

On pages 94 and 95 when defining the apply_vertical_tunnel and apply_horizontal_tunnel the following line casts to a usize. The try_idx method already returns a Option type so the cast is not needed?

self.map.tiles[idx as usize] = TileType::Floor;

That’s a good catch, thank you! It looks like you’re right (at one point it returned a different type, and I missed removing the cast); fortunately it works ok and Rust optimizes it away, but it doesn’t need to be there.

I’ll see if I can get this changed; the book is in the pre-production cycle now, so I’ll have to find out what the process is to let me do that. Since it’s not a breaking issue, it’s not going to be a “stop the presses!” moment - but I believe there’s an edit cycle coming up as part of the process.

Thanks for finding it!

1 Like