Rust By Example: Match Destructuring Enums and Structs
I've discovered my favorite pattern in Rust due to its programming capabilities and syntax. It beautifully combines enum, or enumerations, and the match keyword. These two together form the most powerful and my favorite combination in Rust.
And I was unknowingly doing something called destructuring when tapping into this pattern. You can create an enum, which is a list of possible states. Typically, only one item from this enum will exist per instance.
For example, let's create an enum of colors with variable amounts of data in a tuple format. Most colors are defined with a U8, so a U32 is pretty generous. Once we have our color enum, we can match these enums.
This process is highly readable. Unlike destructuring arrays and tuples which can drop readability, with enums you know what you're matching on. If some of the information is variable, you can destructure the data.
One of the things I love about Rust is the match statement and how it flags warnings if you haven't covered all possible cases. So we can add in the additional colours like magenta and yellow. Now the system works successfully.
We can test it by passing in the RGB pattern and it automatically knows the data type. This makes it extremely readable. Instead of passing the data into a print statement, you can enter it this way and it simply becomes more understandable.
The result has the same amount of syntax and keystrokes, but it's just a tad more intuitive. Thanks to the enum and match statement combination for this seamless process. Now let's talk about destructuring our structs.
The syntax is a bit more complex to read, but it's certainly powerful. It's also a better approach than just destructuring and matching statements with an array or tuple because it offers shorthand. Let me walk you through this.
Let's say we have a structure named foo with an X and a Y element. We'll create an instance of our struct by defining data with a tuple of one, two, and Y three. We can then match that data using the known struct information.
This pattern of destructuring is very clean. The double dot or '..' tells us that we only care about the Y element in the struct. You can't match on something like a match statement without specifying the struct has more information in it.
And you don't need a match statement to destructure a struct. You can do so using the pattern "let foo". In short, Rust lets you do shorthand thanks to the element name in the struct and the idea of shadowing.
In case there's a struct inside a struct, you can even destructure with nesting. It's profoundly powerful, especially when you need it. it's always recommended to avoid this as it makes it complicated to read.
Видео Rust By Example: Match Destructuring Enums and Structs канала Stephen Blum
And I was unknowingly doing something called destructuring when tapping into this pattern. You can create an enum, which is a list of possible states. Typically, only one item from this enum will exist per instance.
For example, let's create an enum of colors with variable amounts of data in a tuple format. Most colors are defined with a U8, so a U32 is pretty generous. Once we have our color enum, we can match these enums.
This process is highly readable. Unlike destructuring arrays and tuples which can drop readability, with enums you know what you're matching on. If some of the information is variable, you can destructure the data.
One of the things I love about Rust is the match statement and how it flags warnings if you haven't covered all possible cases. So we can add in the additional colours like magenta and yellow. Now the system works successfully.
We can test it by passing in the RGB pattern and it automatically knows the data type. This makes it extremely readable. Instead of passing the data into a print statement, you can enter it this way and it simply becomes more understandable.
The result has the same amount of syntax and keystrokes, but it's just a tad more intuitive. Thanks to the enum and match statement combination for this seamless process. Now let's talk about destructuring our structs.
The syntax is a bit more complex to read, but it's certainly powerful. It's also a better approach than just destructuring and matching statements with an array or tuple because it offers shorthand. Let me walk you through this.
Let's say we have a structure named foo with an X and a Y element. We'll create an instance of our struct by defining data with a tuple of one, two, and Y three. We can then match that data using the known struct information.
This pattern of destructuring is very clean. The double dot or '..' tells us that we only care about the Y element in the struct. You can't match on something like a match statement without specifying the struct has more information in it.
And you don't need a match statement to destructure a struct. You can do so using the pattern "let foo". In short, Rust lets you do shorthand thanks to the element name in the struct and the idea of shadowing.
In case there's a struct inside a struct, you can even destructure with nesting. It's profoundly powerful, especially when you need it. it's always recommended to avoid this as it makes it complicated to read.
Видео Rust By Example: Match Destructuring Enums and Structs канала Stephen Blum
Комментарии отсутствуют
Информация о видео
20 марта 2024 г. 19:00:44
00:10:29
Другие видео канала