열거형 분해(역구조화)
튜플과 마찬가지로 구조체 및 enum도 다음을 일치시켜 디스트럭처링할 수 있습니다.
구조체
열거형
구조체나 열거형 값의 일부를 패턴 매치를 통해 변수에 바인딩할 수 있습니다. 간단한 enum
타입을 먼저 살펴보겠습니다:
match
구문에서 divide_in_two
함수에서 반환되는 Result
값을 두 개의 팔(혹은 가지)로 분해(destructure) 하였습니다. 첫번째 팔에서 half
는 Ok
variant에 담긴 값으로 바인딩됩니다. 두번째 팔에서 msg
는 오류 메시지 문자열에 바인딩됩니다.
Speaker Notes
This slide should take about 8 minutes.
구조체
foo
의 리터럴 값을 다른 패턴과 일치하도록 변경합니다.Foo
에 새 필드를 추가하고 필요에 따라 패턴을 변경합니다.- 캡처와 상수 표현식은 구분하기 어려울 수 있습니다. 두 번째 부문의
2
를 변수로 변경해 보고 작동하지 않는 것을 확인하세요.const
로 변경하고 다시 작동하는지 확인합니다.
열거형
키 포인트:
if
/else
표현식은 열거형을 반환하고, 이 값은 나중에match
로 분해됩니다.- 열거형에 세번째 variant를 추가하고 코드를 실행하여 오류를 표시해보세요. 코드 어느 부분에 누락이 있는지, 그리고 컴파일러가 어떤 식으로 힌트를 주는지 같이 살펴보세요.
- The values in the enum variants can only be accessed after being pattern matched.
- Demonstrate what happens when the search is inexhaustive. Note the advantage the Rust compiler provides by confirming when all cases are handled.
- Save the result of
divide_in_two
in theresult
variable andmatch
it in a loop. That won’t compile becausemsg
is consumed when matched. To fix it, match&result
instead ofresult
. That will makemsg
a reference so it won’t be consumed. This “match ergonomics” appeared in Rust 2018. If you want to support older Rust, replacemsg
withref msg
in the pattern.