Skip to content

Instantly share code, notes, and snippets.

@iamjarvo
Forked from bjorndown/enum_display.rs
Created July 31, 2020 14:44
Show Gist options
  • Save iamjarvo/ddda3889820811c50f2d945098ccd37a to your computer and use it in GitHub Desktop.
Save iamjarvo/ddda3889820811c50f2d945098ccd37a 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