Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The if/let statement is a concise way to treat the case of an enum value matching one pattern only.

Let us define a tagged union:

And an array of values:

In the following example the code block of the if/let statement is used twice in order to process the two cases Miles and Km:

Speed is 60.5 km/h.
Speed is 40.2 miles/h.
()

13.1.1Returned value

With the if/let/else expression we may return values, the restriction being that all returned values must be of the same type. Let us use a if/let/else expression to get a speed in km/h all the time:

Speed is 0 km/h.
Speed is 60.5 km/h.
Speed is 64.83871 km/h.
()

13.1.2let/else

The let/else control flow works the other way, letting us match a value and run a code block when it does not match:

Speed is 40.2 miles/h.
()