-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
Pattern matching is where the functional programming roots of Rust shine through the most and is also one of the aspects of the language that allows us to craft APIs that are a joy to use. Pattern matching is used in almost every non-trivial Rust program, and understanding it is essential to be able to read and understand Rust.
Some readers may have wondered why we didn't discuss enumerations earlier, when we were looking at the syntax of structures. The reason is that enumerations (hereafter enums) in Rust are significantly more powerful than those in C++.
At their simplest, Rust enums can easily duplicate the capabilities of C++:
simple_enum.rs
enum Numbers {
Zero,
One,
Two,
}
simple_enum.cpp
enum class Number {
Zero,
One,
Two
};
These two declarations and their semantics are almost identical. I've used enum class in the C++ example because this most...