官术网_书友最值得收藏!

How it works...

Ever since we came across pattern matching in the Scala programming language, we fell in love with its simplicity. As a major pillar of functional programming, the technique provides a quick way to transform values in various ways without sacrificing Rust's type safety. 

The literal matches in Steps 2 and are a great way to save on if-else chains. However, the most common matching is probably done to unpack Result or Option types for extracting the encapsulated values. While multiple matches are only possible using the | symbol , there are special operators to match to particular variations: ... denotes a range, while .. means to skip the remaining members of a struct. _ is almost always a wildcard for ignoring a particular thing and, as a match clause, it is a catch-all and should be placed last. In Step 3, we did a lot of unpacking of tuples; we skipped some of the matches using an _ in place of a variable name.

In a similar fashion, Step 4 sets up and uses Rust's mechanism to match properties inside types when using the match clause (also called destructuring). This feature supports nesting and lets us pick values and sub-structs out of a complex struct instance. Neat! 

However, it is often not done with matching on the type and then working with the unpacked values only in the match arm. Instead, having the match conditions lined up is a much better way to deal with allowed values within types. Rust's match clause supports guards for exactly that reason. Step 5 shows what they are capable of. 

Steps 8 and 9 both then show the use of the previously implemented match functions. We highly recommend going through some experiments yourself and seeing what changes. The typed matching allows for complex architectures without verbose safeguards or workarounds, which is exactly what we want!

We've successfully learned about complex conditions with pattern matching. Now, let's move on to the next recipe.

主站蜘蛛池模板: 汨罗市| 赤城县| 科尔| 吕梁市| 三河市| 安庆市| 高平市| 托里县| 平乐县| 根河市| 合肥市| 张家界市| 成安县| 司法| 唐山市| 陇西县| 罗平县| 班戈县| 涡阳县| 汉源县| 交城县| 绥棱县| 利川市| 拜城县| 阳原县| 贵定县| 乡城县| 东乌珠穆沁旗| 肃北| 榆中县| 汉寿县| 南城县| 香港 | 鄂州市| 新蔡县| 克山县| 固阳县| 会东县| 农安县| 连南| 长治县|