Files
.gitea
.github
.obsidian
HelloWorld
actix_web_learning
adder
branches
functions_rust
guessing_game
hello-async
hello_cargo
loops
minigrep
ownership
rectangles
.gitignore
Advanced Features.md
Advanced Functions and Closures.md
Advanced Traits.md
Advanced Types.md
Any Number of Futures.md
Applying Async.md
Async, Await, Futures and Streams.md
Cargo Workspaces.md
Cargo and Cratesio.md
Characteristics of OO Languages.md
Closures.md
Collection of Common Data Structs.md
Concurrency.md
Constants.md
Crates.md
Custome Build Profiles.md
Data Types.md
Deref Trait.md
Drop Trait.md
Enums.md
Error Handling.md
Extending Cargo.md
For later baja.md
Futures and Async.md
Futures in Sequence.md
Futures, Tasks and Threads Together.md
Generic Types Traits and Lifetimes.md
Generics.md
Hash.md
Implementing OO Design Pattern.md
Improving The IO Project.md
Install Binaries.md
Iterators and Closures.md
Iterators.md
Leaky Reference Cycles.md
Lifetimes.md
Modules and Use.md
OOP Programming Features.md
Packages.md
Passing Data Between Threads.md
Paths.md
Pattern Matching.md
Pattern Syntax.md
Places Patterns Can Be Used.md
Primitives.md
Project Organization.md
Publishing libraries.md
README.md
Reducing_Code_Duplication.md
Ref Cell Mutability.md
Reference Counter Smart Pointer.md
Refutability.md
RustBrock.code-workspace
Shared State Concurrency.md
Simultaneous Code Running.md
Smart Pointers.md
String.md
Structures.md
Sync and Send.md
Test Controls.md
Test_Organization.md
Tests.md
The Performance Closures and Iterators.md
Trait Objects that allow for Values of Different Types.md
Traits for Async.md
Traits.md
Unsafe Rust.md
Using Box on the Heap.md
Variables.md
Vector.md
Writing_Tests.md
data_types.md
ownership.md
RustBrock/Pattern Matching.md

44 lines
1.9 KiB
Markdown

# Patterns and Matching
*Patterns* are a special syntax in Rust for matching against the structure of types, both complex and simple.
Using patterns in conjunction with `match` expressions and other constructs give you more control over a program's flow.
A pattern consists of some combination of the following:
- Literals
- Destructured arrays, enums, structs, or tuples
- Variables
- Wildcards
- Placeholders
Some examples include `x`, `(a, 3)` and `Some(Color::Red)`.
In the contexts in which patterns are valid, these components describe the shape of data.
Our program then matches values against the patterns to determine whether it has the correct shape of data to continue running a particular piece of code.
In order to use a pattern, we compare it to some value.
If the pattern matches the value, we use the value parts in our code.
Recall the `match` expression that used patterns, such as the coin-sorting machine example.
If the value fits the shape of the pattern, we can use the named pieces.
If it doesn't, the code associated with the pattern won't run.
This chapter is intended to be a reference on all things related to patterns.
We will cover:
- Valid places to use patterns [Section Link Here]()
- Difference between refutable and irrefutable patterns [Section Link Here](./Refutability.md)
- Different kinds of pattern syntax [Section Link Here](./Pattern%20Syntax.md)
By the end you will know how to use patterns to express many concepts in a clear way.
## Summary
Patterns are very useful in distinguishing between different kinds of data.
When used in `match` expressions, Rust ensures your patterns cover every possible value, or your program will not compile.
Patterns in `let` statements and function parameters make those constructs more useful, enabling the destructuring of values into smaller parts at the same time as assigning to variables.
Then we can create simple or complex patterns to suit our needs.