บทที่ 7: Enums & Pattern Matching
Enums ช่วยให้เราแสดงค่าที่เป็นไปได้หลายแบบ และ Pattern Matching ช่วยจัดการแต่ละแบบ
สิ่งที่จะได้เรียนรู้
| หัวข้อ | คำอธิบาย |
|---|---|
| การสร้าง Enum | นิยาม variants |
| Option<T> | จัดการค่าที่อาจไม่มี |
| Match | Pattern matching |
| if let | Concise matching |
Enums คืออะไร?
Enum แสดงว่าค่าเป็น หนึ่งใน หลายตัวเลือก:
#![allow(unused)]
fn main() {
enum Direction {
North,
South,
East,
West,
}
}
ค่าของ Direction ต้องเป็น North, South, East, หรือ West เท่านั้น