MATCH in Rust vs. SWITCH

So I decided I actually think I like vertical bar for this:

switch number [
    1 => [print "One!"]
    2 | 3 | 5 | 7 | 11 => [print "This is a prime"]
    in-range 13..19 => [print "A teen"]
    print "Ain't special"  ; leveraging "fallout"...last expression as default
]

Even more rustlike. It has parity with PARSE expressing "alternates", and it leaves comma available for separating clauses, especially important in CASE/CHOOSE when you're putting things on one line.

>> choose (10 / 2) [1 | 2 [print "small"], 3 | 4 | 5 '<big>]
== <big>

Actually... Commas might be good for AND'ing vs. OR'ing

switch number [
    > 20, even? [print "Greater than 20 -and- even"]
    < 20 | odd? [print "Less than 20 -or- odd"]
]

Hard to think of another symbol for that. It puts the AND clauses in sequence--kind of like a parse rule--where all would have to apply.

3 Likes