Enums

7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.48
List Price:  €94.97
You save:  €28.49
£55.48
List Price:  £79.27
You save:  £23.78
CA$98.33
List Price:  CA$140.47
You save:  CA$42.14
A$107.87
List Price:  A$154.11
You save:  A$46.23
S$93.98
List Price:  S$134.26
You save:  S$40.28
HK$544.62
List Price:  HK$778.06
You save:  HK$233.44
CHF 61.72
List Price:  CHF 88.17
You save:  CHF 26.45
NOK kr776.67
List Price:  NOK kr1,109.57
You save:  NOK kr332.90
DKK kr495.83
List Price:  DKK kr708.36
You save:  DKK kr212.53
NZ$118.77
List Price:  NZ$169.68
You save:  NZ$50.90
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,384.83
List Price:  ৳11,978.85
You save:  ৳3,594.01
₹5,909.65
List Price:  ₹8,442.72
You save:  ₹2,533.07
RM310.93
List Price:  RM444.20
You save:  RM133.27
₦118,108.82
List Price:  ₦168,734.12
You save:  ₦50,625.30
₨19,496.15
List Price:  ₨27,852.84
You save:  ₨8,356.68
฿2,416.22
List Price:  ฿3,451.90
You save:  ฿1,035.67
₺2,425.32
List Price:  ₺3,464.89
You save:  ₺1,039.57
B$407.22
List Price:  B$581.77
You save:  B$174.54
R1,267.41
List Price:  R1,810.66
You save:  R543.25
Лв130.20
List Price:  Лв186.01
You save:  Лв55.81
₩97,475.91
List Price:  ₩139,257.28
You save:  ₩41,781.36
₪255.36
List Price:  ₪364.82
You save:  ₪109.45
₱4,110.68
List Price:  ₱5,872.66
You save:  ₱1,761.97
¥10,596.44
List Price:  ¥15,138.42
You save:  ¥4,541.98
MX$1,447.86
List Price:  MX$2,068.47
You save:  MX$620.60
QR255.83
List Price:  QR365.49
You save:  QR109.65
P957.28
List Price:  P1,367.60
You save:  P410.32
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,476.05
List Price:  E£4,966
You save:  E£1,489.95
ብር8,780.73
List Price:  ብር12,544.44
You save:  ብር3,763.70
Kz63,807.13
List Price:  Kz91,156.95
You save:  Kz27,349.82
CLP$68,429.22
List Price:  CLP$97,760.22
You save:  CLP$29,331
CN¥507.13
List Price:  CN¥724.50
You save:  CN¥217.37
RD$4,230.16
List Price:  RD$6,043.35
You save:  RD$1,813.18
DA9,356.77
List Price:  DA13,367.38
You save:  DA4,010.61
FJ$158.89
List Price:  FJ$226.99
You save:  FJ$68.10
Q541.54
List Price:  Q773.67
You save:  Q232.12
GY$14,679.97
List Price:  GY$20,972.29
You save:  GY$6,292.31
ISK kr9,633.42
List Price:  ISK kr13,762.62
You save:  ISK kr4,129.20
DH703.39
List Price:  DH1,004.89
You save:  DH301.49
L1,281.87
List Price:  L1,831.32
You save:  L549.45
ден4,093.12
List Price:  ден5,847.57
You save:  ден1,754.44
MOP$562.43
List Price:  MOP$803.51
You save:  MOP$241.07
N$1,269.65
List Price:  N$1,813.87
You save:  N$544.21
C$2,582.26
List Price:  C$3,689.10
You save:  C$1,106.84
रु9,463.39
List Price:  रु13,519.71
You save:  रु4,056.31
S/264.78
List Price:  S/378.27
You save:  S/113.49
K282.85
List Price:  K404.09
You save:  K121.24
SAR263.02
List Price:  SAR375.76
You save:  SAR112.74
ZK1,934.85
List Price:  ZK2,764.20
You save:  ZK829.34
L330.84
List Price:  L472.66
You save:  L141.81
Kč1,681.68
List Price:  Kč2,402.50
You save:  Kč720.82
Ft27,373.74
List Price:  Ft39,107.03
You save:  Ft11,733.28
SEK kr765.13
List Price:  SEK kr1,093.09
You save:  SEK kr327.96
ARS$70,495.75
List Price:  ARS$100,712.53
You save:  ARS$30,216.78
Bs484.88
List Price:  Bs692.72
You save:  Bs207.83
COP$306,405.11
List Price:  COP$437,740.35
You save:  COP$131,335.24
₡35,854.29
List Price:  ₡51,222.61
You save:  ₡15,368.32
L1,774.67
List Price:  L2,535.36
You save:  L760.68
₲547,554.16
List Price:  ₲782,253.76
You save:  ₲234,699.59
$U2,999.11
List Price:  $U4,284.63
You save:  $U1,285.51
zł286.41
List Price:  zł409.17
You save:  zł122.76
Already have an account? Log In

Transcript

Now it's time to talk about enums. enums in rust are more like algebraic data types in Haskell than C like enums. You specify an enum with a keyword enough the name of the enum in capital camel case, and the names of the variants in a block. You can stop there if you want, and just use it like that, in which case, it is sort of like an enum and see just namespace into the enum and away you go. However, the real power of arrest enough comes from associating data and methods with the variants, you can always have a named variant with no data. A variant can have a single type of data, a tuple of data, or an anonymous struct of data.

And enough is sort of like a union in C only so much better. If you create an enum the value can be any one of these variants. For example, your dispenser item could be an empty with no data associated with it, but you can tell that it's an empty Or it could be an ammo with a single byte in it. Or it could be a things with a string and assigned 32 bit integer in it, or it could be a place with x and y is 30 twos. It can be any one of those but only one at a time. Even better, you can implement functions and methods for an email.

You can also use enums with generics option is a generic enough in the standard library that you will use all the time the T and angle brackets means any type, you don't have to use t you could use some other valid identifier but the idiomatic thing to do in rust is to use T or some other capital letter. The option enough represents when something is either absent or present. If you're trying to reach for a no or nil value, like in other languages, you probably want to use an option in rust. You either have some value wrapped into some variant or you have none. I'm going to use an option for the rest of the examples in this Section. Because enums can represent all sorts of data, you need to use patterns to examine them.

If you want to check for a single variant, you use the if let expression, if let takes a pattern that will match one of the variants. If the pattern does match, then the condition is true and the variables inside the pattern are created for the scope of the if left block. If the pattern doesn't match, then the condition is false. This is pretty handy if you care about one variant matching or not, but not as great if you need to handle all the variants at once. In that case, you use the match expression which is match a variable whose type supports matching like an EM the body of the match and braces where you specify patterns followed by double arrows which are equal signs followed by greater than symbols pointing to an expression that represents the return value of that arm of the match. Match expressions require you to write a branch arm for every possible outcome.

In other words, the patterns in a match expression must be exhaustive. A single underscore all by itself is a pattern that matches anything and can be used for a default or anything else branch. Note that even though you will often see blocks as the expression for a branch arm any expression will do, including things like function calls and bear values. Either all branch arms need to return nothing or all branch arms need to return the same type. Remember that if you actually use the return value of an expression that ends in a curly brace like match if left or if or a nested block, then you have to put a semicolon after the closing brace. If you don't use the return value of a braced expression, then rust lets you cheat and leave off the semicolon.

I want to talk a bit more in depth about two special enums. What's so special about them is that they're used all over the standard library so you will encounter them constantly first. Let's look a little more at option. Here's the definition of option. Again, as I said earlier option is used whenever something might be absent. Here's how you could create a nun variant of an option.

I specified the type that some will wrap an angle brackets after the option. Notice that I don't have a use statement bringing into scope option or its variants, some or none from the standard library. Since option and its variants are used so much. They're already included in the standard Prelude which is the list of items from the standard library that are always brought into scope by default. If you ever use option with a concrete type, then the compiler will infer the type which means you can leave the type annotation off of the declaration most of the time there's a handy helper method called is sum that returns true if x is the sum variant. There's also an is none method that does just the opposite option implements the into iterator trait.

So you can also treat it similar To a vector of zero or one items and put it in a for loop, you ought to read through the methods for option because you'll end up using them a lot. There are a whole bunch more of them that I won't go over. The other important em is result result is used whenever something might have a useful result or might have an error. This turns up especially often in the i o module. Here's the definition of the result enum First, you'll see that the type wrapped by okay and the type rep by air are both generic but independent of each other. Second, the must use annotation makes it a compiler warning to silently drop a result you have to do something with it rust strongly encourages you to look at all possible errors and make a conscious choice what to do with each one.

Anytime you deal with IO failure is a possibility. So results are used a lot there. Like I said earlier. Let's see it in action. Here I bring standard file system file and disk scope and then Try to open a file. This returns a result because the file might not get opened successfully.

Since I dropped the result without doing anything with it, I get this compiler warning, unused standard result result that must be used. The point is ignoring errors is not a safe thing to do. So let's go to something to do with our result, the simplest thing you could choose to do is to unwrap the result with the unwrap method. If the result isn't Okay, then this gives you the file structure that you wanted. If the result is an error, then this crashes the program. In some cases crashing the program may be what you want.

In any case, you get to choose another option is the expect method. It's exactly the same as unwrap except that the string that you pass to expect is also printed in the crash output. So you can provide yourself a little bit of custom context as to why the crash occurred. Just like option there helper methods like is okay and is error that return Boolean Here we know that unwrap will never crash because we've made sure it wasn't okay already. Of course, you can always do fall pattern matching as well. Here I match on a result and execute different blocks depending on what I got back.

In the next video, we'll talk about closures.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.