Replies: 1 comment
-
This is a good idea since enums are used all over the place in Rust and less common in Python. This could also be a place to introduce traits as a more advance topic after the initial introduction to enums. One thing that took me a minute to figure out was this difference: In Python class Color(Enum):
RED = "red"
BLUE = "blue"
print(Color.RED.value) This doesn't work in Rust, instead you implement the enum Color {
Red,
Blue,
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Color::Red => write!(f, "red"),
Color::Blue => write!(f, "blue"),
}
}
}
fn main() {
println!("{}", Color::Red);
} Before I learned about implementing this trait I thought it just wasn't possible to have the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm thinking about ways to introduce the concept of Enums using similar data to what exists in other pieces in this repo. Something along the lines of people and their metadata, so as to not deviate too far from the norm. There's tons of Enum examples online regarding video games or other such "creative" examples (wizards, mages and such), but I think the focus of this book should be a pragmatic approach, and sometimes, it's necessary to define boring Enums for boring problems 😀...
Just putting this down here to list out some ideas @sanders41.
Beta Was this translation helpful? Give feedback.
All reactions