1

I wrote the following code :

macro_rules! my_macro{
        ("A") => {
                println!("Macro called !")
        }
}

fn main(){
        static test: &'static str = "A";
        my_macro!(test);
}

but I have the following error :

error: no rules expected the token `test`
 --> test.rt:9:19
  |
1 | macro_rules! my_macro{
  | --------------------- when calling this macro
...
9 |         my_macro!(test);
  |                   ^^^^ no rules expected this token in macro call

error: aborting due to previous error

However, it works fine if I directly call my_macro("A"). Is it possible to fix this ?

0

2 Answers 2

5

Is it possible to fix this ?

No. Macros are expanded at compile time before item names are resolved, therefore your macro has no idea what the value of test is (and would have no idea even if it were a const rather than a static).

Sign up to request clarification or add additional context in comments.

Comments

0

so the first problem here is that you macro expects a pattern of "A" not a variable that contains "A"

when you create macros you define certain patterns and follow those patterns in your case your macro must always have "A" in it but it is not a string a it is a pattern of double quote followed by capital a followed by another double quote

If you want to pass a value you should use variable syntax and define what it should expect such as ($a:expr)=>{...}

here you can see all magic tokens possible just scroll down a bit on that docs there are a lot of great examples

PS. here is a macro I use for responding from my endpoints

macro_rules! resp {
    (ok) => {
        |_| actix_web::HttpResponse::Ok().body(r#"{"success":true}"#)
    };
    (ok,$data:expr) => {
        |_| actix_web::HttpResponse::Ok().json(serde_json::json!({"success":true,"data":$data}))
    };
    (ok,) => {
        |d| actix_web::HttpResponse::Ok().json(serde_json::json!({"success":true,"data":d}))
    };
}

4 Comments

I do not want to pass a value but rather to filter on different pattern (for example, does it start with a A, or end with a C ?). However, macro does not seem to support conditions :(
actually if does inside macro rules you can create different cases I will add photo of a macro I use to respond to users from my endpoints to the answer
Thanks, however what I was trying to achieve is to create different result based on what my constant string contained. It seems that your solution produce different result based on whether or not some argument is passed
Well i understand what you're trying to do but macro expressions have no clue what is rust code and have no means of verifying it since macros are expanded before any rust code is actually checked so macros cannot check runtime logic even if the variable contains static data it is still not known at the point when macros expand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.