Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did Rust go with "match" instead of "switch" or "case"?

Tags:

rust

I'm curious about the history of this. I would assume switch would appeal to C/C++/Java/etc programmers and thus be a natural choice. If one wanted to avoid confusion in semantics (since match is more powerful than switch) I'd assume one would borrow case from Haskell instead.

like image 430
tibbe Avatar asked Oct 23 '16 23:10

tibbe


People also ask

Does rust have switch case?

It is mainly used in a menu-driven program where the user selects which function they want to run. A switch case in Rust is achieved by the keyword match.

Is match the same as switch?

Match is similar to a switch statement but it is a lot more powerful. A match works on integers, ranges of integers, bools, enums, tuples, arrays and structs. It will destructure tuples, arrays and structs. It requires a default handler if necessary.

What does match do in Rust?

Rust has an extremely powerful control-flow operator called match that allows us to compare a value against a series of patterns and then execute code based on which pattern matches.

What is switch and match?

match and switch are just convenient ways of writing long if-else statements for increasing readability and making code more robust etc. Usually, match is used for pattern matching and also it can be a statement and as well as expression whereas switch is used for equality checking and is statement only.


1 Answers

The Rust Reference cites Standard ML (SML) and OCaml as an influence for pattern matching. In OCaml, pattern matching uses the match keyword.

The original Rust compiler was written in OCaml, so it makes sense that OCaml would have had a stronger influence on Rust than, say, Haskell.

like image 163
Francis Gagné Avatar answered Sep 27 '22 20:09

Francis Gagné