How to create a vec of enum with a macro

use std::any::Any;

#[derive(Debug, PartialEq, Eq)]
struct F1 {}

#[derive(Debug, PartialEq, Eq)]
enum A {
    F(F1),
    S(String),
}

macro_rules! create_var {
    ($($var:expr),+) => {{
        //@todo:how to do it?


      //vec![
       //     $(A::F($var)),+
        //]

    }};
}

fn main() {
    assert_eq!(
        create_var!("a", "b"),
        vec![A::S("a".to_string()), A::S("b".to_string())]
    );
    assert_eq!(create_var!(F1 {}, F1 {}), vec![A::F(F1 {}), A::F(F1 {})]);
}

Can rust do it?

It's not a purely macro solution, but I'd probably use From here.

2 Likes

Good idea ,thx.

Hi @shanliu. I suggest changing the title to "How to create a vec of enum with a macro". And please select the reply from Quinedot as the accepted solution if it is the solution.