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
€65.70
List Price:  €93.87
You save:  €28.16
£56.78
List Price:  £81.12
You save:  £24.34
CA$96.02
List Price:  CA$137.18
You save:  CA$41.15
A$108.71
List Price:  A$155.31
You save:  A$46.59
S$95.36
List Price:  S$136.23
You save:  S$40.87
HK$548.45
List Price:  HK$783.54
You save:  HK$235.08
CHF 63.80
List Price:  CHF 91.15
You save:  CHF 27.34
NOK kr769.93
List Price:  NOK kr1,099.95
You save:  NOK kr330.02
DKK kr490.31
List Price:  DKK kr700.47
You save:  DKK kr210.16
NZ$118.43
List Price:  NZ$169.19
You save:  NZ$50.76
د.إ257.05
List Price:  د.إ367.23
You save:  د.إ110.18
৳7,676.37
List Price:  ৳10,966.71
You save:  ৳3,290.34
₹5,834.67
List Price:  ₹8,335.60
You save:  ₹2,500.93
RM334.41
List Price:  RM477.75
You save:  RM143.34
₦90,777.03
List Price:  ₦129,687.03
You save:  ₦38,910
₨19,466.05
List Price:  ₨27,809.84
You save:  ₨8,343.78
฿2,589.56
List Price:  ฿3,699.53
You save:  ฿1,109.97
₺2,277.64
List Price:  ₺3,253.91
You save:  ₺976.27
B$364.49
List Price:  B$520.72
You save:  B$156.23
R1,335.85
List Price:  R1,908.44
You save:  R572.59
Лв128.52
List Price:  Лв183.62
You save:  Лв55.09
₩96,509.16
List Price:  ₩137,876.14
You save:  ₩41,366.97
₪262.81
List Price:  ₪375.46
You save:  ₪112.65
₱4,024.28
List Price:  ₱5,749.22
You save:  ₱1,724.94
¥10,831.24
List Price:  ¥15,473.86
You save:  ¥4,642.62
MX$1,193.35
List Price:  MX$1,704.86
You save:  MX$511.51
QR255.21
List Price:  QR364.60
You save:  QR109.39
P971.48
List Price:  P1,387.89
You save:  P416.41
KSh9,343.66
List Price:  KSh13,348.66
You save:  KSh4,005
E£3,370.40
List Price:  E£4,815.06
You save:  E£1,444.66
ብር3,974.28
List Price:  ብር5,677.79
You save:  ብር1,703.50
Kz58,484.22
List Price:  Kz83,552.47
You save:  Kz25,068.24
CLP$67,138.60
List Price:  CLP$95,916.40
You save:  CLP$28,777.80
CN¥506.95
List Price:  CN¥724.24
You save:  CN¥217.29
RD$4,127.56
List Price:  RD$5,896.78
You save:  RD$1,769.21
DA9,431.64
List Price:  DA13,474.35
You save:  DA4,042.71
FJ$159.10
List Price:  FJ$227.30
You save:  FJ$68.19
Q544.01
List Price:  Q777.19
You save:  Q233.18
GY$14,634.05
List Price:  GY$20,906.69
You save:  GY$6,272.63
ISK kr9,876.98
List Price:  ISK kr14,110.58
You save:  ISK kr4,233.60
DH708.59
List Price:  DH1,012.31
You save:  DH303.72
L1,249.26
List Price:  L1,784.74
You save:  L535.47
ден4,044.46
List Price:  ден5,778.05
You save:  ден1,733.58
MOP$564.32
List Price:  MOP$806.20
You save:  MOP$241.88
N$1,343.97
List Price:  N$1,920.04
You save:  N$576.07
C$2,574.58
List Price:  C$3,678.13
You save:  C$1,103.54
रु9,342.18
List Price:  रु13,346.54
You save:  रु4,004.36
S/260.97
List Price:  S/372.84
You save:  S/111.86
K265.79
List Price:  K379.72
You save:  K113.93
SAR262.52
List Price:  SAR375.05
You save:  SAR112.52
ZK1,795.86
List Price:  ZK2,565.63
You save:  ZK769.76
L326.99
List Price:  L467.14
You save:  L140.15
Kč1,660.63
List Price:  Kč2,372.43
You save:  Kč711.80
Ft25,906.43
List Price:  Ft37,010.78
You save:  Ft11,104.34
SEK kr762.37
List Price:  SEK kr1,089.15
You save:  SEK kr326.77
ARS$60,978.49
List Price:  ARS$87,115.87
You save:  ARS$26,137.37
Bs484.36
List Price:  Bs691.97
You save:  Bs207.61
COP$274,870.93
List Price:  COP$392,689.60
You save:  COP$117,818.66
₡35,126.73
List Price:  ₡50,183.20
You save:  ₡15,056.46
L1,726.83
List Price:  L2,467.01
You save:  L740.17
₲517,471.24
List Price:  ₲739,276.32
You save:  ₲221,805.07
$U2,687.21
List Price:  $U3,839.04
You save:  $U1,151.82
zł283.69
List Price:  zł405.30
You save:  zł121.60
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.