3 Code Flow

 

This chapter covers

  • Discussing pattern matching
  • Handling errors with pattern matching
  • Reviewing Rust’s functional programming patterns

We need to continue to review more of Rust’s core language features–its building blocks–before diving into design patterns. In this chapter, we’ll start by discussing pattern matching and functional programming. Pattern matching allows us to control the code flow, unwrap or destructure values, and handle optional cases. Functional programming lets us build software around the unit of a function, which is one of the most basic and easiest-to-understand abstractions.

These building blocks are distinct but can be combined in many ways to create entirely new abstractions. We’ll tie these building blocks together to create more elaborate design patterns by combining them in various ways. To use an analogy, in cooking, we employ four essential elements in different combinations from multiple sources to create delicious foods: salt, fat, acid, and heat. Before making patterns based on these elements, we must understand them in depth.

3.1 A Tour of Pattern Matching

3.1.1 Basics of pattern matching

3.1.2 Clean matches with the ? operator

3.2 Functional Rust

3.2.1 Basics of functional programming in Rust

3.2.2 Closure variable capture

3.2.3 Examining iterators

3.2.4 Obtaining an iterator with iter(), into_iter(), and iter_mut()

3.2.5 Iterator features

3.3 Summary

sitemap