Enums and Pattern Matching

  • In this chapter, we’ll look at enumerations, also referred to as enums. Enums allow you to define a type by enumerating its possible variants. First we’ll define and use an enum to show how an enum can encode meaning along with data. Next, we’ll explore a particularly useful enum, called Option, which expresses that a value can be either something or nothing. Then we’ll look at how pattern matching in the match expression makes it easy to run different code for different values of an enum. Finally, we’ll cover how the if let construct is another convenient and concise idiom available to handle enums in your code.

    • 이 장에서는 열거형(enum)이라고도 함)을 살펴봅니다. 열거형은 가능한 변형을 열거하여 유형을 정의할 수 있습니다. 먼저 열거형을 정의하고 사용하여 데이터와 함께 의미를 인코딩하는 방법을 보여드리겠습니다. 다음으로 값이 무언가가 될 수도 있고 아무것도 아닐 수도 있음을 표현하는 옵션이라는 특히 유용한 열거형을 살펴봅니다. 그런 다음 일치 표현식의 패턴 매칭을 통해 열거형의 다른 값에 대해 다른 코드를 쉽게 실행할 수 있는 방법을 살펴봅니다. 마지막으로, 코드에서 열거형을 처리할 수 있는 또 다른 편리하고 간결한 관용구가 어떻게 구성되는지 살펴봅니다.
  • Enums Advenced(중급이상내용)