Skip to content

Commit

Permalink
Fix rust-lang#1253: Document enum type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Sep 13, 2019
1 parent dbcdccf commit b854723
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/custom_types/enum.md
Expand Up @@ -53,6 +53,43 @@ fn main() {
```

## Type aliases

If you use a type alias, you can refer to each enum variant via its alias.

```rust,editable
enum VeryVerboseEnumOfThingsToDoWithNumbers {
Add,
Subtract,
}
// Creates a type alias
type Operations = VeryVerboseEnumOfThingsToDoWithNumbers;
fn main() {
// We can refer to each variant via its alias, not its long and inconvenient name.
let x = Operations::Add;
}
```

The most common place you'll see this is in `impl` blocks using the `Self` alias.

```rust,editable
enum VeryVerboseEnumOfThingsToDoWithNumbers {
Add,
Subtract,
}
impl VeryVerboseEnumOfThingsToDoWithNumbers {
fn run(&self, x: i32, y: i32) -> i32 {
match self {
Self::Add => x + y,
Self::Subtract => x - y,
}
}
}
```

### See also:

[`match`][match], [`fn`][fn], and [`String`][str]
Expand Down

0 comments on commit b854723

Please sign in to comment.