1
votes

Can i do something like

macro_rules! problem_call {
    ($x:expr) => {
        problem_x();
    }
}

So i can call a series of functions that have names like: (problem_1, problem_2, problem_3, ...)

with a variable like: problem_call(2) or maybe problem_call('2')

(This code obviously doesn't work, but is there any way to reproduce something like that?)

1
I think it's only possible with procedural macro or build script ofc. You can't count or do anything like that in declarative macro thus the name. - Stargateur
Does this answer your question? Is there a way to count with macros? - Stargateur
I'm unsure I understand your requirement can you give more example or clarify the question somehow ? - Stargateur
Not exacly, but i think a procedural macro solve my question. I ll read more about, thanks - Gobbs

1 Answers

0
votes

Yes, it is possible using concat_idents in Rust Nightly.

https://doc.rust-lang.org/std/macro.concat_idents.html

This should be something like what you're looking for:

#![feature(concat_idents)]

macro_rules! problem_call {
    ($x:ident) => {
        concat_idents!(problem_, $x)();
    }
}

fn problem_abc() {
    println!("abc");
}

problem_call!(abc);