Enums
- Enums are a way to define a type by enumerating a list of variants.
enum IpAddrKind {
V4,
V6,
}
fn main() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
}#[derive(Debug)]
enum IpAddrKind {
V4,
V6,
}
struct IpAddr {
kind: IpAddrKind,
address: String,
}
fn main() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
let localhost = IpAddr {
kind: IpAddrKind::V4,
address: String::from("127.0.0.1"),
};
}Values Associated with Enums
- Enums can have values associated with each variant.
enum IpAddrKind {
V4(u8, u8, u8, u8),
V6(String),
}
fn main() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
let localhost = IpAddrKind::V4(127, 0, 0, 1);
}- Instead of using a struct, we can use an enum to define the
IpAddrtype.
#[derive(Debug)]
enum IpAddrKind {
V4(u8, u8, u8, u8),
V6(String),
}
#[derive(Debug)]
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
impl Message {
fn call(&self) {
println!("{:?}", self);
}
}
fn main() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
let localhost = IpAddrKind::V4(127, 0, 0, 1);
}
Enum Methods
#[derive(Debug)]
enum IpAddrKind {
V4(u8, u8, u8, u8),
V6(String),
}
impl IpAddrKind {
fn call(&self) {
println!("{:?}", self);
}
}
fn main() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
let localhost = IpAddrKind::V4(127, 0, 0, 1);
localhost.call(); // V4(127, 0, 0, 1)
}The Option Enum
- Rust does not have a
nullvalue, instead has anOptionenum is defined by the standard library. enum Option<T> { Some(T), None }
fn main() {
let some_number = Some(5);
let some_string = Some("a string");
let absent_number: Option<i32> = None;
}Optionis a generic enum,Tis the type of the value thatSomevariant will hold.
fn main() {
let x: i8 = 5;
let y: Option<i8> = Some(5);
let sum = x + y;
}⛔
Error: cannot add i8 to Option<i8>
fn main() {
let x: i8 = 5;
let y: Option<i8> = Some(5);
let sum = x + y.unwrap_or(0);
}unwrap_orreturns the value ifSomevariant, otherwise returns the default value.
Match Expressions
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("Lucky penny!");
1
}
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
Arizona,
California,
// --snip--
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("Lucky penny!");
1
}
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
}
}
}
fn main() {
let coin = Coin::Quarter(UsState::California);
let value = value_in_cents(coin);
println!("The value of the coin is {} cents.", value);
}matchis used to compare the value of an enum to a list of possible values.
fn main() {
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
println!("{:?}, {:?}, {:?}", five, six, none);
}
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
Some(i) => Some(i + 1),
None => None,
}
}The _ Placeholder
- The
_placeholder is used to match all remaining cases.
fn main() {
let some_u8_value = 0u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
}if let Syntax
if letsyntax is a shorter way to write amatchthat only matches one case.
fn main() {
let some_u8_value = Some(0u8);
match some_u8_value {
Some(3) => println!("three"),
_ => (),
}
if let Some(3) = some_u8_value {
println!("three");
}
}