From 020711fa97e3be57f9e0098d6b9a329deec5a754 Mon Sep 17 00:00:00 2001 From: mo8it Date: Fri, 21 Jun 2024 23:05:40 +0200 Subject: [PATCH] enums3 solution --- exercises/08_enums/enums2.rs | 2 +- rustlings-macros/info.toml | 2 +- solutions/08_enums/enums2.rs | 27 ++++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/exercises/08_enums/enums2.rs b/exercises/08_enums/enums2.rs index f3b803ff..14aa29ad 100644 --- a/exercises/08_enums/enums2.rs +++ b/exercises/08_enums/enums2.rs @@ -1,6 +1,6 @@ #[derive(Debug)] enum Message { - // TODO: define the different variants used below + // TODO: Define the different variants used below. } impl Message { diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index 7535b682..a9921045 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -451,7 +451,7 @@ dir = "08_enums" test = false hint = """ You can create enumerations that have different variants with different types -such as no data, anonymous structs, a single string, tuples, ...etc""" +such as no data, anonymous structs, a single string, tuples, etc.""" [[exercises]] name = "enums3" diff --git a/solutions/08_enums/enums2.rs b/solutions/08_enums/enums2.rs index 4e181989..13175dd3 100644 --- a/solutions/08_enums/enums2.rs +++ b/solutions/08_enums/enums2.rs @@ -1 +1,26 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +#[derive(Debug)] +enum Message { + Move { x: i64, y: i64 }, + Echo(String), + ChangeColor(u8, u8, u8), + Quit, +} + +impl Message { + fn call(&self) { + println!("{:?}", self); + } +} + +fn main() { + let messages = [ + Message::Move { x: 10, y: 30 }, + Message::Echo(String::from("hello world")), + Message::ChangeColor(200, 255, 255), + Message::Quit, + ]; + + for message in &messages { + message.call(); + } +}