평생을 struct 가 주류이고 enum 이 보조인 형태의 언어만 써봤으니 이런 패턴의 코드가 이해가 안되던 것이었어. enum 타입의 한 부분을 struct 로 보면 아래 코드가 읽히는 것이다.

// [1] 선택지가 여러 개인 경우 (일반적인 Enum)
pub type Shape {
  Circle(radius: Float)             // 원
  Rectangle(width: Float, height: Float) // 사각형
}

// [2] 선택지가 하나뿐인 경우 (우리가 아는 Struct)
pub type User {
  User(name: String, age: Int)      // 유저는 무조건 이름과 나이를 가짐
}

sum 타입 기본 언어를 잘 쓰려면 역시 데이터 모델링을 잘 해야겠구나.

2

If you have a fediverse account, you can quote this note from your own instance. Search https://hackers.pub/ap/notes/019ad8ea-b5fd-7db7-8294-72edf52dc058 on your instance and quote it. (Note that quoting is not supported in Mastodon.)