Macro with parameter Option


I try to pass an option argument to an macro_rules. Some(x) is all right, but the None does create compiler error (async fn). I declared the Option argument as $abc:expr.

macro_rules! my_marco1 {

You have not included enough information for us to help you. Please post a link the playground that contains your macro, and that displays the error in question when you click "build".

Also please properly format your code:

1 Like

This isn't actually an issue with your macro - it's an issue with the expression it generates. See this example.

The problem in this situation is that the compiler has no way to infer the type of the variable x in the Some(x) pattern. Even though that pattern can be proven to be unreachable in this specific instance, that's not true in general, and the compiler is concerned about the general case.

In order to make this work, the compiler needs a type hint to understand what the Some case could carry. Fixed example.

Your macro would work fine if the parameter were anything that let the compiler infer the type. If it's a literal None, though, that information doesn't exist. Try passing None with an explicit type parameter instead.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.