Questions tagged [rust-decl-macros]

28 questions
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
13
votes
1 answer

What does an @ symbol mean in a Rust declarative macro?

I have seen the @ symbol used in macros but I cannot find mention of it in the Rust Book or in any official documentation or blog posts. For example, in this Stack Overflow answer it is used like this: macro_rules! instructions { (enum…
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
12
votes
1 answer

Import macro from parent module

I'm having trouble re-using macros within a crate. If a macro is defined in ./src/macros.rs: #[macro_export] macro_rules! my_macro { ... } and used in ./src/lib.rs: #[macro_use] pub mod macros; I can't see this macro in…
marcusklaas
  • 492
  • 5
  • 15
7
votes
1 answer

Build all pairs of elements (quadratic set) in declarative macro

I have a list of identifier and I want to invoke a macro for each pair of identifiers from that list. For example, if I have a, b and c, I would like to generate this: println!("{} <-> {}", a, a); println!("{} <-> {}", a, b); println!("{} <-> {}",…
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
6
votes
0 answers

How to make a macro expand before stringifying it in Rust?

I'm trying to write a quine in Rust using only macros. In order to do that, I'm embedding the main function into macro f1, and trying to embed a literal representation of f1 in f2 with stringify!. Here is my code so far: macro_rules!f1{()=>(fn…
Jon Nimrod
  • 335
  • 1
  • 2
  • 12
6
votes
1 answer

Pass entire macro input to another macro

I'm trying to make a simple macro that invokes vec! with whatever it receives then does some simple processing before returning the new vector: macro_rules! sorted_vec { ($x:expr) => { { let v = vec![$x]; …
anderspitman
  • 9,230
  • 10
  • 40
  • 61
5
votes
2 answers

How can I generate trait bounds in a declarative macro?

I have a trait with a large number of associated types. I want a function that uses those associated types on both sides of a where clause bound: trait Kind { type A; type B; // 20+ more types } trait Bound {} fn example
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
5
votes
1 answer

How to write a macro that displays the file and line number and a variable number of arguments?

I've found several useful macros in Rust, namely: file!(), line!(), stringify!() I've also found that Rust allows macros with variable arguments, as stated here: macro_rules! print_all { ($($args:expr),*) => {{ $( …
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
4
votes
1 answer

How to return a new String from a declarative macro?

So here I am, trucking along with Rustlings, until I get broadsided with test 4. It wants me to write a macro that will satisfy the following code: fn main() { if my_macro!("world!") != "Hello world!" { panic!("Oh no! Wrong output!"); …
AerosolSP
  • 177
  • 1
  • 1
  • 10
3
votes
1 answer

How to parse single tokens in rust macros

I'm starting playing with Rust macros and I came to try this little practice example. I want to define a macro that expands into a variable initialization (name doesn't matter) of type i32 (for example, but not really important) and a series of…
Netwave
  • 40,134
  • 6
  • 50
  • 93
2
votes
1 answer

How do I detect if an optional keyword was supplied to a declarative macro?

I'm writing a macro: macro_rules! foo { ($(print)?) => { // run `println!("hello") if print is given } } Which could be called as: foo!() which would do nothing foo!(print) which would print hello How do I detect if print was…
lolad
  • 321
  • 6
  • 13
2
votes
1 answer

How to match a trait in a Rust macro?

My goal is to take as input trait type. my_test_macro!(Trait1, Trait2) What I tried so far was writing parser like this. $( $ty:ident < $( $N:ident $(: $b0:ident $(+$b:ident)* )? ),* $($tname:ident=$ttype:ident),* > )+* But it…
Inline
  • 2,566
  • 1
  • 16
  • 32
1
vote
2 answers

How do I early return in declarative macros and infer the return type?

I have a macro, which just generates an instance of a struct, as below: macro_rules! foo { () => {{ let baz_val = baz(); let bar_val = match bar() { Ok(val) => val, Err(err) => { …
Eray Erdin
  • 2,633
  • 1
  • 32
  • 66
1
vote
1 answer

How do I generate a macro from previous macro-generated code?

In order to create a pyo3-powered Python class working with a struct that uses generic type, I want to use wrappers that will generate the code needed to not have to do it for every specific type. I created a macro that generates the code but I need…
litchipi
  • 53
  • 1
  • 5
1
vote
2 answers

How do I get an identifer from an expression argument in a macro?

I have a constant value defined by a variable: const VAL: usize = 32; I want to make a function like this: macro_rules! valfn { ($val:expr) => { pub fn $val () -> () { // here val needs to be a ident some_other_fn($val) …
user318904
  • 2,968
  • 4
  • 28
  • 37
1
2