rustwasm / book

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

section 4.4 final exercise problem with impl Display #266

Open Lazerbeak12345 opened 2 years ago

Lazerbeak12345 commented 2 years ago

Where in the docs did you come across this?

In section 4.4, in the last exercise (the one where it recommends use of FixedBitSet for storing alive/dead state).

Describe what about it does not make sense

During the adjustments in that answer, it doesn't touch the impl fmt::Display for Universe block, so it can't compile as that block needs updating or removal to still work.

Why does it not make sense?

It isn't clear that the impl fmt::Display for Universe block should be removed or tampered (it makes only a passing mention "Also, instead of rendering Unicode text, we'll switch to using the Canvas API.").

How could we improve it?

I have two ideas:

  1. Recommended This can be fixed during the "Rendering to Canvas Directly from Memory" section. We could add somewhere in that section to remove said block
  2. We could include how to fix the code:
use std::fmt;
impl fmt::Display for Universe {
   fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
       for row in 0..self.height { // changed
           for col in 0..self.width { // changed
               let idx = self.get_index(row, col); // changed
               let cell = self.cells[idx]; // changed
               let symbol = if cell { '◼' } else { '◻' }; // changed
               write!(f, "{}", symbol)?;
           }
           write!(f, "\n")?;
       }
       Ok(())
   }
}

I recommend solution 1 over 2 because the effected code is dead code.