How do I conditionally check if an enum is one variant or another?
Asked Answered
C

3

66

I have an enum with two variants:

enum DatabaseType {
    Memory,
    RocksDB,
}

What do I need in order to make a conditional if inside a function that checks if an argument is DatabaseType::Memory or DatabaseType::RocksDB?

fn initialize(datastore: DatabaseType) -> Result<V, E> {
    if /* Memory */ {
        //..........
    } else if /* RocksDB */ {
        //..........
    }
}
Cyler answered 19/7, 2018 at 18:22 Comment(0)
C
114

First have a look at the free, official Rust book The Rust Programming Language, specifically the chapter on enums.


match

fn initialize(datastore: DatabaseType) {
    match datastore {
        DatabaseType::Memory => {
            // ...
        }
        DatabaseType::RocksDB => {
            // ...
        }
    }
}

if let

fn initialize(datastore: DatabaseType) {
    if let DatabaseType::Memory = datastore {
        // ...
    } else {
        // ...
    }
}

==

#[derive(PartialEq)]
enum DatabaseType {
    Memory,
    RocksDB,
}

fn initialize(datastore: DatabaseType) {
    if DatabaseType::Memory == datastore {
        // ...
    } else {
        // ...
    }
}

matches!

This is available since Rust 1.42.0

fn initialize(datastore: DatabaseType) {
    if matches!(datastore, DatabaseType::Memory) {
        // ...
    } else {
        // ...
    }
}

See also:

Chimene answered 19/7, 2018 at 18:29 Comment(1)
It's silly that in order to check one variant of the enum with ==, you have to derive(PartialEq) while it does essentially the same as the other examples. Also, the if let<type> = <value> syntax is completely counter-intuitive. Assigning a value to a type doesn't make sense. The fact that they created the matches! macro, kind of proves my point.Heckelphone
A
3
// A simple example that runs in rust 1.58:
enum Aap {
    Noot(i32, char),
    Mies(String, f64),
}

fn main() {
    let aap: Aap = Aap::Noot(42, 'q');
    let noot: Aap = Aap::Mies(String::from("noot"), 422.0);
    println!("{}", doe(aap));
    println!("{}", doe(noot));
}

fn doe(a: Aap) -> i32 {
    match a {
        Aap::Noot(i, _) => i,
        Aap::Mies(_, f) => f as i32,
    }
}
Ayers answered 14/2, 2022 at 16:35 Comment(1)
I don't know if you want to modify your example, but it's worth pointing out that if one of the variants is a struct with named fields (say, Noot { a: i32, b: char }) then one matches using .. instead of _.Identity
B
2

As of Rust 1.21.0, you can use std::mem::discriminant to check against an enum variant instead of by value:

use std::mem;

enum Foo { A(&'static str), B(i32), C(i32) }

assert_eq!(mem::discriminant(&Foo::A("bar")), mem::discriminant(&Foo::A("baz")));
Benediction answered 8/12, 2023 at 1:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.