Skip to content

String literal macros #16876

Closed
Closed
@UberLambda

Description

@UberLambda

Currently, some constructs like pattern matching using stringify! on the left hand side of a match arm are not applicable, since the macro doesn't produce string literals - it produces expressions instead.

Example

#![feature(macro_rules)]

macro_rules! my_match {
    ($to_match:ident, $($var:ident,)*) => {
        match $to_match {
            $(
                stringify!($var) => { // error: non-pattern macro in pattern position: stringify
                    $var = 42;
                }
            )*
            _ => {
                fail!("Failure");
            }
        }
    }
}

fn main() {
    let a_var = 32;
    let b_var = 43;
    let c_var = 44;

    let to_match = "a_var";

    my_match! { to_match,
        a_var,
        b_var,
        c_var,
    };
}

Such macros could be useful for code similiar to the above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions