create account

Rust lang series episode #9 — enums (#rust-series) by jimmco

View this thread on: hive.blogpeakd.comecency.com
· @jimmco · (edited)
Rust lang series episode #9 — enums (#rust-series)
![](https://ipfs.pics/ipfs/QmRewcCALeid2GMkjouKTcYcTwVoJ4XcfrxnUjMQ55q25L)

Hello everyone, here is a new episode of [Rust series]((https://steemit.com/created/rust-series)). Today, we are going to talk about enumeration types in Rust.

# Enums
Enum is enumeration type similar to enums in other languages. It represents one of several possible values called variants. In Rust enums are just little bit more powerful and there are multiple possibilities how enums can be used. Let's take a look at them. 

## Variants with no data
```rust
enum Switch {
  On,
  Off
}
```

## Variants with unnamed data (like tupples)
```rust
enum Move {
    Left(i32),
    Right(i32),
    Up(i32),
    Down(i32)
}
```

## Variants with named data (like strusts)
```rust
enum Change {
    None,
    Move {dx: i32, dy: i32}
}
```

## Enum variables
Enum variables can have one of the variants values, they are assigned with **::** because they are enum scoped.

```rust
let switch = Switch::On;
let move = Move::Up(10);
let change = Change::Move{x: 10, y: 0};

// don't forget to add #[derive(Debug)] before enum definittions
println!("{:?}", switch);
println!("{:?}", move);
println!("{:?}", change);

# output
On
Up(10)
Move { x: 10, y: 0 }
```

## Evaluating with match
We can evaluate enum value by **match** statement

```rust
match switch {
    State::On => println!("on"),
    State::Off => println!("off")

# output
on
}
```
## Getting sub-values
We can also access variant sub-values with certain destructuring in match expression, see the example:
```rust
match change {
    Change::Move{x, y} => println!("x: {}, y: {}", x, y),
    _ => panic!()
}

# output
x: 10, y: 0
```

Note: panic! macro causes application exit and prints panic message

As you can see enums are very practical and can be used in almost any Rust application. 

## Postfix
That's all for today, thanks for all appreciations, feel free to comment and point out possible mistakes (first 24 hours works the best but any time is fine). God bless your programming skills and stay tuned for next episodes.

Meanwhile you can also check the official documentation to find more about enums:  
* https://doc.rust-lang.org/book/enums.html
* http://rustbyexample.com/custom_types/enum.html


[#rust-series](https://steemit.com/created/rust-series)
[#rust-lang](https://steemit.com/created/rust-lang)
[#rust](https://steemit.com/created/rust)
👍  , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorjimmco
permlinkrust-lang-series-episode-9-enums-rust-series
categoryrust-series
json_metadata{"tags":["rust-series","rust-lang","rust","programming","enums"],"image":["https://ipfs.pics/ipfs/QmRewcCALeid2GMkjouKTcYcTwVoJ4XcfrxnUjMQ55q25L"],"links":["(https://steemit.com/created/rust-series)","https://doc.rust-lang.org/book/enums.html","http://rustbyexample.com/custom_types/enum.html","https://steemit.com/created/rust-series","https://steemit.com/created/rust-lang","https://steemit.com/created/rust"]}
created2016-09-28 07:10:24
last_update2016-09-28 09:05:33
depth0
children0
last_payout2016-10-29 07:58:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,394
author_reputation9,585,502,514,260
root_title"Rust lang series episode #9 — enums (#rust-series)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,382,936
net_rshares119,947,534,292
author_curate_reward""
vote details (25)