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
9
votes
1 answer

How do I provide helpful compiler errors in a procedural macro?

I am designing a custom HTML syntax parser using proc_macro and syn. A sample: #[derive(Debug)] struct BlockElement { stag: Ident, child: Vec, ctag: Ident } impl Synom for BlockElement { named!(parse -> Self, do_parse!( …
pepsighan
  • 798
  • 9
  • 17
8
votes
1 answer

"found struct `ThereIsNoIteratorInRepetition`" when trying to repeat over a vector using `quote!`

I'm trying to create a Vec of TokenStreams, and then use that list in another quote! macro: let list: Vec<_> = some_data .iter() .map( |item| { quote!{/*...*/} }, ) …
Reinis Mazeiks
  • 1,168
  • 1
  • 10
  • 21
8
votes
0 answers

Alternative to ctor/inventory for when compiling to wasm?

The ctor crate doesn't support web assembly currently, although there is active discussion about how to fix this. Although I am well aware of the issues associated with static initialization coming from C++, being able to register things with a…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
8
votes
1 answer

Custom literals via Rust macros?

Is it possible in Rust to define a macro that can parse custom literals, e.g. something along the lines of vector!(3x + 15y) To clarify, I would like to be able to get as close to the above syntax as one can (within the realm of what is possible…
MrMobster
  • 1,851
  • 16
  • 25
8
votes
1 answer

Convert string into TokenStream

Given a string (str), how can one convert that into a TokenStream in Rust? I've tried using the quote! macro. let str = "4"; let tokens = quote! { let num = #str; }; // #str is a str not i32 The goal here is to generate tokens for some unknown…
lovelikelando
  • 7,593
  • 6
  • 32
  • 50
7
votes
0 answers

#[should_panic] doesn't accept constant as expected panic message

When running rust unit tests it is very useful to utilize the attribute macro #[should_panic(expect = )] to assert that the test is panicking with the correct error message (which means it's panicking at the line you want it to panic and not because…
João A. Veiga
  • 498
  • 3
  • 11
7
votes
2 answers

Getting "error[E0599]: no method named write_fmt found" error in Rust tutorial code

I'm trying to write to standard output using the writeln!() instead of the println!() macro, so I can handle I/O errors (e.g. when I pipe long-running output to head) gracefully. I found the following snippet at…
Huw Walters
  • 1,888
  • 20
  • 20
7
votes
1 answer

write! macro does not compile in a separate method when taking reference

This is my code: use std::fs::File; use std::io::Write; fn main() { let f = File::create("").unwrap(); // Compiles write!(&f, "hi").unwrap(); write_hi(&f); } fn write_hi(f: &File) { // Doesn't compile (cannot borrow `f` as…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
7
votes
4 answers

How to simplify mathematical formulas with rust macros?

I must admit I'm a bit lost with macros. I want to build a macro that does the following task and I'm not sure how to do it. I want to perform a scalar product of two arrays, say x and y, which have the same length N. The result I want to compute is…
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
1 answer

"unnecessary parens" warning in macro call that needs the parens - is this a bad way to write macros?

This is an extremely minor issue and I know how to do disable the warning, but reading up on it I suspect it might indicate that I might be doing something incorrect with my macro. Anyway, I have a struct Rational for rational…
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
6
votes
2 answers

Is there a way to get the type of a variable in a macro?

I have an procedural attribute macro which given a function operates on each binary expression e.g. let a = b + c; and returns another expression depending on it. With the + operation depending on the types, it needs to know the types of a, b and…
Jonathan Woollett-light
  • 2,813
  • 5
  • 30
  • 58
6
votes
1 answer

Is macro_rules a regular macro?

I'm trying to understand rust macro syntax. Here I read that macro may be invoked generally in 3 flavours: mymacro!(); mymacro![]; mymacro!{}; ...and then I see an example macro definition also using macro (macro_rules),…
ardabro
  • 1,907
  • 16
  • 31
6
votes
1 answer

Rust macro for repeating array elements

I'm trying to write a Rust macro that fills up an array with repeating elements, in this case with zeros. This is what I came up with: macro_rules! pad4 { () => { println!("0b00000000, 0b00000000, 0b00000000, 0b00000000"); …
Andor Polgar
  • 483
  • 3
  • 13
6
votes
1 answer

How to create a macro that matches enum variants without knowing its structure?

I've found the following solution to create a macro that defines a function which returns true if an enum matches a variant: macro_rules! is_variant { ($name: ident, $enum_type: ty, $enum_pattern: pat) => { fn $name(value: &$enum_type)…
Alvra
  • 353
  • 2
  • 10
1 2
3
20 21