Questions tagged [rust-macros]

Rust macros are a powerful tool to map a certain input sequence to an output sequence according to a defined procedure.

There are currently two types of macros in Rust:

310 questions
188
votes
5 answers

How do I use a macro across module files?

I have two modules in separate files within the same crate, where the crate has macro_rules enabled. I want to use the macros defined in one module in another module. // macros.rs #[macro_export] // or not? is ineffectual for this,…
user
  • 4,920
  • 3
  • 25
  • 38
86
votes
4 answers

How do I see the expanded macro code that's causing my compile error?

I have a compile error involving a macro: :6:19: 6:50 error: cannot move out of captured outer variable in an `FnMut` closure :6 bind ( $ e , move | $ p | mdo ! { $ ( $ t ) * } ) ) ; ( …
Caspar
  • 7,039
  • 4
  • 29
  • 41
43
votes
1 answer

How to write a custom derive macro?

I'm trying to write my own derive mode macro in Rust, and the documentation on it is somewhat lacking in examples. I have a struct like: #[derive(MyMacroHere)] struct Example { id: i64, value: Option, } I want my macro to generate a…
Lev
  • 1,698
  • 3
  • 18
  • 26
40
votes
3 answers

What does #[macro_use] before an extern crate statement mean?

In Rust, I sometimes see #[macro_use] before an extern crate statement: #[macro_use] extern crate gotham_derive; What does this do compared to not having #[macro_use]? extern crate gotham_derive;
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
39
votes
1 answer

What does the tt metavariable type mean in Rust macros?

I'm reading a book about Rust, and start playing with Rust macros. All metavariable types are explained there and have examples, except the last one – tt. According to the book, it is a “a single token tree”. I'm curious, what is it and what is it…
Pavel Davydov
  • 3,379
  • 3
  • 28
  • 41
32
votes
3 answers

How to report errors in a procedural macro using the quote macro?

I am writing a procedural macro which works fine, but I am having trouble reporting errors in an ergonomic way. Using panic! "works" but is not elegant and does not present the error message to the user nicely. I know that I can report good errors…
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
30
votes
2 answers

How to programmatically get the number of fields of a struct?

I have a custom struct like the following: struct MyStruct { first_field: i32, second_field: String, third_field: u16, } Is it possible to get the number of struct fields programmatically (like, for example, via a method call…
Akiner Alkan
  • 6,145
  • 3
  • 32
  • 68
29
votes
1 answer

Why do proc-macros have to be defined in proc-macro crate?

I was trying to create a derive macro for my trait, to simplify some stuff. I've encountered some problems: the #[proc_macro_derive] attribute is only usable with crates of the proc-macro crate type and, after the small fix…
majkrzak
  • 1,332
  • 3
  • 14
  • 30
27
votes
3 answers

How do I debug macros?

So I've got the following macro code I'm trying to debug. I've taken it from the Rust Book under the section "The deep end". I renamed the variables within the macro to more closely follow this post. My goal is to have the program print out each…
Nashenas
  • 1,651
  • 1
  • 21
  • 25
22
votes
1 answer

How to make a public struct where all fields are public without repeating `pub` for every field?

How can I define a public struct in Rust where all the fields are public without having to repeat pub modifier in front of every field? A pub_struct macro would be ideal: pub_struct! Foo { a: i32, b: f64, // ... } which would be…
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
22
votes
1 answer

How to call methods on self in macros?

macro_rules! call_on_self { ($F:ident) => { self.$F() } } struct F; impl F { fn dummy(&self) {} fn test(&self) { call_on_self!(dummy); } } The above doesn't work (Playground): error[E0424]: expected value, found…
colinfang
  • 20,909
  • 19
  • 90
  • 173
20
votes
1 answer

Is it possible to store state within Rust's procedural macros?

Is it possible to build a macro that doesn't output anything but instead for example stores state to build up a list and then a second macro that will then actually use that data and output some result? For example: trait SomeTrait…
Thermatix
  • 2,757
  • 21
  • 51
18
votes
4 answers

Is there a way to count with macros?

I want to create a macro that prints "Hello" a specified number of times. It's used like: many_greetings!(3); // expands to three `println!("Hello");` statements The naive way to create that macro is: macro_rules! many_greetings { …
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
17
votes
1 answer

How to replace one identifier in an expression with another one via Rust macro?

I'm trying to build a macro that does some code transformation, and should be able to parse its own syntax. Here is the simplest example I can think of: replace!(x, y, x * 100 + z) ~> y * 100 + z This macro should be able to replace the first…
hoheinzollern
  • 651
  • 6
  • 16
16
votes
1 answer

Why can I not access a variable declared in a macro unless I pass in the name of the variable?

I have this macro: macro_rules! set_vars { ( $($x:ident),* ) => { let outer = 42; $( let $x = outer; )* } } Which expands this…
rincewind
  • 1,103
  • 8
  • 29
1
2 3
20 21