Andrew Wagner
Andrew Wagner

Reputation: 24547

Can a single Rust macro generate multiple declarations?

As a learning exercise I am trying to write a macro that generates two declarations. In this example I am trying to write a macro that generates declarations for an enum with a single field and a static vector that contains an instance of that field:

#![feature(macro_rules)]
macro_rules! create_enum(
        ( $enum_name : ident , $a_field_name : ident ) => 
        {
            enum $enum_name { $a_field_name };
            static foovec: [$enum_name,..1] = [ $a_field_name ]; 
        };
)

create_enum! (Direction , NORTH)

I get the error:

enums.rs:5:36: 5:37 error: macro expansion ignores token `;` and any following
enums.rs:5              enum $enum_name { $a_field_name };

I have tried maybe 10 punctuation variations without success, so I am starting to wonder if this just isn't supported by rust macros.

Upvotes: 2

Views: 565

Answers (1)

Dogbert
Dogbert

Reputation: 222198

enum declarations don't need a ; at the end.

This works for me:

#![feature(macro_rules)]
macro_rules! create_enum(
        ( $enum_name : ident , $a_field_name : ident ) => 
        {
            enum $enum_name { $a_field_name }
            static foovec: [$enum_name,..1] = [ $a_field_name ]; 
        };
)

create_enum! (Direction , NORTH)

Demo: http://is.gd/JxMAb1

Upvotes: 3

Related Questions