rustwasm / book

The Rust and WebAssembly Book
https://rustwasm.github.io/docs/book/
MIT License
1.75k stars 211 forks source link

Game of life live_neighbor_count wrong neighbour index #177

Closed tinoism closed 5 years ago

tinoism commented 5 years ago

The function in live_neighbor_count of Game of life should iterate delta_* from [-1, 0, 1] instead of from [self.height - 1, 0, 1] and [self.width - 1, 0, 1].

tinoism commented 5 years ago

Just realized that it wouldn't compile... Although the intuition about having the negative index is correct, in rust we annotate the negative indices by adding the length of the array. For instance, "negative indexes like -1 and -2 get added to the length of the slice to become real indices, like len - 1 and len - 2" as indicated in this issue.