2

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?)

Gobbs
  • 23
  • 3
  • 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 Dec 27 '19 at 16:19
  • Does this answer your question? [Is there a way to count with macros?](https://stackoverflow.com/questions/33751796/is-there-a-way-to-count-with-macros) – Stargateur Dec 27 '19 at 16:20
  • I'm unsure I understand your requirement can you give more example or clarify the question somehow ? – Stargateur Dec 27 '19 at 16:23
  • Not exacly, but i think a procedural macro solve my question. I ll read more about, thanks – Gobbs Dec 27 '19 at 16:27

1 Answers1

1

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);
Brandon Dyer
  • 1,316
  • 12
  • 21