Skip to content

Commit

Permalink
Merge pull request #1255 from adamchalmers/achalmers/alias-enum
Browse files Browse the repository at this point in the history
Fix #1253: Document enum type aliases
  • Loading branch information
marioidival committed Sep 16, 2019
2 parents dbcdccf + d3fe7d1 commit f49e432
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/custom_types/enum.md
Expand Up @@ -53,11 +53,56 @@ fn main() {
```

## Type aliases

If you use a type alias, you can refer to each enum variant via its alias.
This might be useful if the enum's name is too long or too generic, and you
want to rename it.

```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,
}
}
}
```

To learn more about enums and type aliases, you can read the
[stabilization report][aliasreport] from when this feature was stabilized into
Rust.

### See also:

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

[c_struct]: https://en.wikipedia.org/wiki/Struct_(C_programming_language)
[match]: ../flow_control/match.md
[fn]: ../fn.md
[str]: ../std/str.md
[aliasreport]: https://github.com/rust-lang/rust/pull/61682/#issuecomment-502472847

0 comments on commit f49e432

Please sign in to comment.