Skip to content

Instantly share code, notes, and snippets.

@ndrsllwngr
Forked from bjorndown/enum_display.rs
Created June 22, 2021 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndrsllwngr/2e5a45fde10bd5dbf269687995cf0120 to your computer and use it in GitHub Desktop.
Save ndrsllwngr/2e5a45fde10bd5dbf269687995cf0120 to your computer and use it in GitHub Desktop.
Rust: Implement fmt::Display for enum
use std::fmt;
#[derive(Copy,Clone)]
enum CellState { Dead, Alive }
impl fmt::Display for CellState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let printable = match *self {
CellState::Alive => 'x',
CellState::Dead => ' ',
};
write!(f, "{}", printable)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment