You add methods to enums in Rust by implementing an inherent impl block directly on the enum type. This allows you to define functions that take self (or &self, &mut self) and operate on the enum's variants.
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
impl Message {
fn call(&self) {
match self {
Message::Quit => println!("Quitting"),
Message::Move { x, y } => println!("Moving to {}, {}", x, y),
Message::Write(text) => println!("Text message: {}", text),
Message::ChangeColor(r, g, b) => println!("Color is now {}, {}, {}", r, g, b),
}
}
}
fn main() {
let m = Message::Write(String::from("hello"));
m.call();
}