File tree 1 file changed +37
-0
lines changed 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,43 @@ fn main() {
53
53
54
54
```
55
55
56
+ ## Type aliases
57
+
58
+ If you use a type alias, you can refer to each enum variant via its alias.
59
+
60
+ ``` rust,editable
61
+ enum VeryVerboseEnumOfThingsToDoWithNumbers {
62
+ Add,
63
+ Subtract,
64
+ }
65
+
66
+ // Creates a type alias
67
+ type Operations = VeryVerboseEnumOfThingsToDoWithNumbers;
68
+
69
+ fn main() {
70
+ // We can refer to each variant via its alias, not its long and inconvenient name.
71
+ let x = Operations::Add;
72
+ }
73
+ ```
74
+
75
+ The most common place you'll see this is in ` impl ` blocks using the ` Self ` alias.
76
+
77
+ ``` rust,editable
78
+ enum VeryVerboseEnumOfThingsToDoWithNumbers {
79
+ Add,
80
+ Subtract,
81
+ }
82
+
83
+ impl VeryVerboseEnumOfThingsToDoWithNumbers {
84
+ fn run(&self, x: i32, y: i32) -> i32 {
85
+ match self {
86
+ Self::Add => x + y,
87
+ Self::Subtract => x - y,
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
56
93
### See also:
57
94
58
95
[ ` match ` ] [ match ] , [ ` fn ` ] [ fn ] , and [ ` String ` ] [ str ]
You can’t perform that action at this time.
0 commit comments