Loading tutorials
match compares one value against a list of patterns and runs the code for the first pattern that fits. This chapter covers how match works: its syntax, how it evaluates, and the rules that follow from it being an expression. It assumes you can read basic Rust, know how enums with data are declar5 min readmatch, and read a Rust compiler error. Here you learn what the compiler checks when you write a match, how to keep that check useful, and which errors you will meet.4 min readmatch. It also assumes you know what a move and a borrow are. Here you attach functions to an enum with an impl block, choose the right self receiver for each one, and avoid the errors that4 min readmatch over its variants, and read basic ownership errors such as "use of moved value". The previous chapters covered those topics. Here you will see how the standard library uses them to represent a value that might not exist.5 min readmatch over an enum and know that arms are tried in order. It covers the simplest pattern kinds: ones that test a value against fixed shapes. They work on integers, characters, tuples, and enum variants alike.4 min readmatch, and know the difference between moving and borrowing a value. It covers how patterns take values apart: tuples, structs, and enums nested inside each other.5 min readmatch over an enum, destructure variants that carry data, and read range and or-patterns. You should also know the difference between matching a value and matching a reference. Two extra features cover cases that patterns alone cannot express: **match gua5 min readmatch expressions, know that Option<T> is an enum with Some and None, and understand moves and references at a basic level. A full match is the general tool for taking an enum apart. Often you only care about one variant, and a full match with a throwawa5 min readOption<T> says a value may be missing. Result<T, E> says an operation may fail, and it carries the reason. This chapter covers how Result is built, how to handle it, and how the ? operator removes most of the repetitive error handling. It assumes you know the match expression, Option, an4 min readResult<T, E>, the ? operator on simple cases, match on enums, and how to write impl blocks and implement a trait. It shows how to define one error enum for a whole function or module, and how From lets ? convert errors for you.4 min readmatch expressions with guards and @ bindings, and understand moves versus borrows. It applies those pieces to one design problem. You describe what a system can be (its states) and what can happen to it (its commands), 4 min readBox fixes that, and how to match on boxed values. You will also see when ano4 min readmatch over it, and that you know how Result and the TryFrom trait are used at a basic level.5 min read