Rust
Variants with data
Define enum variants that carry data.
Variants with data
The real power of Rust enums is that each variant can carry different data:
enum Mesaj {
Cik, // no data
Tasi { x: i32, y: i32 }, // struct form
Yaz(String), // tuple form
Renk(u8, u8, u8),
}
let m = Mesaj::Tasi { x: 3, y: -1 };
Had we written four separate structs instead, gathering them into one type and passing them to the same function would be hard. The enum unites them all in one type.
Unpacking the data
In match arms the variant's data is bound to variables:
fn anlat(m: &Mesaj) -> String {
match m {
Mesaj::Cik => String::from("çıkış"),
Mesaj::Tasi { x, y } => format!("taşı {x},{y}"),
Mesaj::Yaz(metin) => format!("yaz: {metin}"),
Mesaj::Renk(r, g, b) => format!("renk {r}/{g}/{b}"),
}
}
When matching on &Mesaj, the bound variables are references too; ownership doesn't move.
Exercise Describe the messages
Write the Mesaj enum above and the function fn anlat(m: &Mesaj) -> String. Loop over a vector containing Cik, Tasi { x: 3, y: -1 }, Yaz("selam") and Renk(0, 128, 255), and print each on its own line.
Show example solution
enum Mesaj {
Cik,
Tasi { x: i32, y: i32 },
Yaz(String),
Renk(u8, u8, u8),
}
fn anlat(m: &Mesaj) -> String {
match m {
Mesaj::Cik => String::from("çıkış"),
Mesaj::Tasi { x, y } => format!("taşı {x},{y}"),
Mesaj::Yaz(metin) => format!("yaz: {metin}"),
Mesaj::Renk(r, g, b) => format!("renk {r}/{g}/{b}"),
}
}
fn main() {
let mesajlar = vec![
Mesaj::Cik,
Mesaj::Tasi { x: 3, y: -1 },
Mesaj::Yaz(String::from("selam")),
Mesaj::Renk(0, 128, 255),
];
for m in &mesajlar {
println!("{}", anlat(m));
}
}
Test yourself
-
How do you reach the text inside <code>Mesaj::Yaz(String::from("a"))</code>?
- With m.0
- With m.Yaz
- With as String
- By pattern matching with match or if let
Show answer
By pattern matching with match or if let — Variant data is only safely unpacked by pattern matching; the compiler needs to know the variant really is Yaz.
-
Struct variant
let m = Mesaj::Tasi ____;
Show answer
let m = Mesaj::Tasi { x: 3, y: -1 };
-
Variant and form
- Cik
- No data
- Tasi { x, y }
- Struct form
- Yaz(String)
- Tuple form, one field
- Renk(u8, u8, u8)
- Tuple form, three fields
Next lesson: Option<T>