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

How to distinguish different kinds of items in macro_rules macros?

I want to write a macro_rules based macro that will be used to wrap a series of type aliases and struct definitions. I can match on "items" with $e:item, but this will match both aliases and structs. I would like to treat the two separately (I need…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
0
votes
1 answer

How do I create a macro that transforms inputs into a tuple?

I want to avoid creating many numbered functions and duplicated code if possible. I'm writing a program that is parsing a config file containing lines like the following and I want to simplify my logic for parsing it with some helper functions. I…
quittle
  • 856
  • 2
  • 10
  • 19
0
votes
1 answer

Comma separated list of syn::Idents

I want to add some syn::Ident's to form a &'static str. All Idents should be concat to form a comma separated and stringified (because that's how I get a string representation of an Ident) &'static str. I got a non-working project here:…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
0
votes
1 answer

Is it possible to write a rust macro that transposes a matrix?

I am currently working on optimizing the rust jpeg decoder crate using SIMD. In order to avoid long repetitions in the code, I would like to write a macro that generates the following matrix transposition code : s = [ …
lovasoa
  • 6,419
  • 1
  • 35
  • 45
0
votes
1 answer

Can I define a macro which will expand into a function call?

I've (naively) tried this, but it doesn't print anything to the screen: macro_rules! foo { ($suffix:tt, $arg:expr) => { concat!("foo", $suffix, "(", $arg, ")"); }; } fn foo_i32(x: i32) { println!("i32 {}", x); } fn foo_bool(x:…
Shmoopy
  • 5,334
  • 4
  • 36
  • 72
0
votes
0 answers

How can I procedurally construct the value of an attribute at compile time?

I have number of source files (001.rs, 002.rs, 003.rs, ...), each having the following content: // FILE: 001.rs pub const NUM: i64 = 1; // Different number per file I would like to load all these values with a macro: // FILE: main.rs macro_rules!…
magu_
  • 4,766
  • 3
  • 45
  • 79
0
votes
0 answers

Is it possible to iterate over an external struct's members in a macro?

I'm working on a Rust app that needs to compile for WebAssembly, which means exposing quite a lot of internal & external structs for use from TypeScript. I want to write a macro to automate this job as it's quite tedious. I want is a macro…
GScrivs
  • 865
  • 1
  • 7
  • 19
0
votes
2 answers

Rustlings Course Test4.rs Marcro Return Type Issue

I am doing the Rustlings Rust-lang course and working on exercises/test4.rs This is the only exercise in the course that does not come with a hint. So after working on it for a while, I'm reaching out to get that hint here! macro_rules! my_macro { …
HMLDude
  • 1,547
  • 7
  • 27
  • 47
0
votes
1 answer

Can I detect if an expression is a mutable variable using Rust's macro repetition?

macro_rules! log { ($($x:expr),*) => { { $( //how to detect $x in the Macro repetition? )* } }; } I don't want to use ($($x:ident),*). The log! macro can log the expression and then I…
CS QGB
  • 297
  • 1
  • 3
  • 12
0
votes
2 answers

Macro to build enum with different "kinds" of elements

I am trying to come up with a macro which I would call like create_states!(S0, S1, final S2, final S3); It will create an enum to represent state machine states and some of them will be final (accepting) states - S2, S3. The resulting enum and its…
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
0
votes
1 answer

Rust println! problem - weird behavior inside the println macro

I am currently working on a simple "user input" program. The user can enter a number, which I get with std::io::stdin().read_line(&mut let_name_here).ok().expect("Error");. After getting the user input I want to print it to the console for a…
0
votes
1 answer

Store state in macro_rules

I'd like to create a macro that operates on a given list of types, but I need to be able to store which other types are being processed. A simple example of something I want to do: struct Foo; struct Bar { foo: Foo, data: u32, } baz!(Foo,…
lesurp
  • 343
  • 4
  • 19
0
votes
0 answers

Why format! cannot be used in the return statement?

This compiles: fn fmt(url: &str) -> String { const base_url: &'static str = "https://api.github.com/"; let req_url = format!("{}{}", &base_url, &url); req_url } fn main() { let url = fmt("bla/bla"); println!("{}", url) } This…
Keeper Hood
  • 594
  • 5
  • 17
0
votes
2 answers

Tuple indexing in macro

I'm trying to index data tuple in macro that generate signature for trait implementation, but have some errors. Can I index tuple or need another solution? Hack with tuple_index I found in google but it not works for…
0
votes
0 answers

Use included text as macro argument

I'm trying to create the possibility to parse C header file at compile-time (and generate a bunch of numeric constants to be used in other code). I don't need to get all the header file constructs parsed, since I control its contents and know that…
Cerberus
  • 8,879
  • 1
  • 25
  • 40
1 2 3
20
21