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

How to disable "unnecessary path disambiguator" warning?

I am generating code with a macro, which contains fully qualified type paths like this: let vec: Vec::; Note the extra :: before . This is necessary so that the same input token can be also be used for the constructor, by appending…
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
5
votes
1 answer

How do I evaluate expressions in Rust's macro system?

I'm trying to learn the Rust macro system by writing a simple macro that generates a struct based on some unsigned integer type (u8, u16, u32, u64). I want something like this: bitmessage! { struct Header(u16); version: 8, 5; // the first…
user8725011
5
votes
2 answers

Is there a macro or similar workaround to include the source folder (src) path at compile time?

Is there a Rust macro or a similar workaround to include the path of the 'src' folder created via cargo new in my source file as a string literal at compile time or specifically when doing cargo build? I have successfully done something similar…
Harindaka
  • 4,658
  • 8
  • 43
  • 62
5
votes
1 answer

Unused variable in macro-generated code

I have written a macro that implements Scala-like for comprehensions in Rust. It will turn this: map_for!{ x <- 0..4; y = 2*x; z <- 0..1; => y+z } into this: ((0..4).map (move |x| { let y = 2 * x; (x, y) })) .flat_map (move…
Jmb
  • 18,893
  • 2
  • 28
  • 55
5
votes
2 answers

How to initialize a struct with a series of arguments

In many languages, a common constructor idiom is to initialize values of an object using syntax like this pseudocode: constructor Foo(args...) { for arg { object.arg = arg } } Rust at first seems to be no exception. Many impl for a…
Aaron3468
  • 1,734
  • 16
  • 29
4
votes
1 answer

Is there any performance difference between macros and functions in Rust?

In Rust Macros are executed at compile time. They generally expand into new pieces of code that the compiler will then need to further process. But after macros compiled or before compiled is there any performance difference between normal…
NsaNinja
  • 154
  • 11
4
votes
0 answers

How do I use attributes on fields on a custom derive macro?

I'm writing my derive macro: use proc_macro::{self, TokenStream}; #[proc_macro_derive(MyMacro, attributes(my_attr))] pub fn my_macro(input: TokenStream) -> TokenStream { ... TokenStream::from(my_generated_code) } with the attributes which will…
4
votes
1 answer

Is there a way to access variables defined in rust macros from a passed expr?

Assuming I want to make the following macro work: macro_rules! process_numbers { ($name:ident, $process:expr) => { let $name: Vec<_> = vec![0, 1, 2].iter().map(|num| { println!("{}", path); // dummy preprocessing …
Todd J. York
  • 135
  • 2
  • 12
4
votes
2 answers

Can Rust macros be shared across editions?

Say a Rust 2018 macro defines an async function inside it. The syntax it would use would be incompatible with Rust 2015. So if you're compiling your crate with 2015 edition, wouldn't this expanded code from the macro conflict with that? I'm not that…
zombiesauce
  • 1,009
  • 1
  • 7
  • 22
4
votes
0 answers

How can I compute the number of functions annotated as #[test] in a crate at compile time?

Background I am writing integration tests using the guidelines in the Rust Book. A requirement is to be able to run setup and teardown code at two levels: Test level: Before and after each test. Crate level: Before and after all tests in the…
David Sackstein
  • 500
  • 1
  • 5
  • 19
4
votes
1 answer

How can I assign "metadata" to a trait?

I have 2 macros. The first is special_trait, an attribute macro to be used on trait declarations, and the second, useful_macro is used with such traits. That is, the user code would write: #[special_trait] pub trait MyTrait{} // meanwhile, in a…
Reinis Mazeiks
  • 1,168
  • 1
  • 10
  • 21
4
votes
1 answer

Is it possible to ignore Rust's exponents?

I am trying to use the letter "E" within a Rust macro without triggering mathematical exponents. Here is an example: macro_rules! test { (0e) => { // Do something }; } fn main() { test!(0e); } This gives the error error: expected at…
sone
  • 43
  • 5
4
votes
1 answer

Creating a Custom Colored dbg! Macro In Rust

I'd like to create a custom macro similar to the standard dbg! macro, but with the option to use colors via the colored crate. dbg! usually prints something with the format of [path_to_file:line_number] "symbol name" = "symbol…
ANimator120
  • 2,556
  • 1
  • 20
  • 52
4
votes
1 answer

Rustlang: Adding a return statement in a macro

I'm going through the rustlings course in order to learn rustlang and I'm working on quiz 4. The following is the solution I found. macro_rules! my_macro { ($val:expr) => { format!("Hello {}", $val) } } #[cfg(test)] mod tests { …
geofflittle
  • 437
  • 1
  • 3
  • 14
4
votes
1 answer

Is it possible to pass a variable as an identifier in a macro?

I want to build a macro that will create functions based upon a configuration file. I would like to be able to invoke that function by name at runtime by passing an argument i.e. a command pattern. For example: macro_rules! create_fn { …
Mike Nishizawa
  • 1,410
  • 1
  • 14
  • 14