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

Is there a way to view expanded macros on non-nightly Rust?

I realise this has been asked before and the answer was to use the -Z option with rustc, but that only works with the nightly build of Rust as of Rust 1.31. Is there an alternative when using the stable build?
John
  • 1,593
  • 3
  • 17
  • 28
0
votes
1 answer

Matching against the enclosing function's return type

Is it possible for a Rust macro to match against the enclosing function's return type? An example is something like a logging and assert macro which also returns Err in functions returning Result and panics in functions not returning Result. To…
nalply
  • 26,770
  • 15
  • 78
  • 101
0
votes
1 answer

Rust macros: how to create a derived identifier?

It looks like for identifiers I can either use an existing token of ident kind, or create a fixed / literal identifier. But how to create a "derived" identifier? Meaning, having an existing token $id, I want to create some derived identifier like…
rincewind
  • 1,103
  • 8
  • 29
0
votes
1 answer

Creating mixed recursive macro to create a HTML templating DSL

I'm new to Rust and even more to the macro engine, I'm trying to come up with a way of creating a DSL that I'll use for HTML templating that looks like the following, h! { foo( "bar", tag_with_parens(), tag_without_parens, "some…
olanod
  • 30,306
  • 7
  • 46
  • 75
0
votes
1 answer

How to implement the Lispian cond macro?

Intended usage: cond! { x > 5 => 0, x < 3 => 1, true => -1 } Should expand to: if x > 5 { 0 } else if x < 3 { 1 } else if true { -1 } Note that it doesn't produce a catch-all else { ... } suffix. My attempt: macro_rules! cond( …
Dominykas Mostauskis
  • 7,797
  • 3
  • 48
  • 67
0
votes
1 answer

How do I test if a identifier in a macro starts with an underscore?

I need to check whether the identifier begins with an underscore. This does not work: #[macro_export] macro_rules! UNDECORED { (_$_i:ident) => {... do something if underscored}; ($_i:ident) => {... do something else}; } Where is the…
Aleksandr
  • 3
  • 1
0
votes
1 answer

Unexpected end of macro invocation in Rust

I have the following code in Rust use std::fmt; pub struct MyRange { pub start: Idx, pub end: Idx, } impl fmt::Debug for MyRange { fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result { write!( "Nothing seriously"…
Omar Abid
  • 15,753
  • 28
  • 77
  • 108
0
votes
1 answer

Macro error while trying to implement a trait inside a macro expansion

fn a() {} seems to satisfy a parsing rule that expected a fn, then some other stuff. items should be able to be function definitions, right? So they should work, right? macro_rules! multi_impl { (for $base:ty : $($t:ty { …
YourGamerMom
  • 377
  • 3
  • 13
-1
votes
1 answer

Rust macro to generate a macro

I'm writing some macros that are all really similar (MWE the actual ones are much larger): macro_rules! var1 { () => {some_func(some_enum::Var1, vec![])}; ($($e:expr),*) => {some_func(some_enum::Var1, vec![$($s),*])}; } macro_rules! var2 { ()…
nxe
  • 241
  • 2
  • 9
-1
votes
1 answer

Different separators for the same variable in Rust Macros

I want to match a pattern like: foo!(1,2,3;4,5,6;7,8,9); The same code would be generated for all numbers, but I'd want additional code to run when there's a semi-colon. Is this sort of pattern possible? I've tried: macro_rule! { foo…
Fuujin
  • 480
  • 5
  • 9
1 2 3
20
21