Questions tagged [macro-rules]
20 questions
6
votes
2 answers
Using a Rust macro to generate a function with variable parameters
I've created a Rust macro which expands to a function declaration.
macro_rules! build_fn
{
($name:tt) => {
pub fn $name(&self) -> Result>
{
//
Ok(0)
…

IanS
- 151
- 2
- 8
2
votes
1 answer
Attempt to implement sscanf in Rust, failing when passing &str as argument
Problem:
Im new to Rust, and im trying to implement a macro which simulates sscanf from C.
So far it works with any numeric types, but not with strings, as i am already trying to parse a string.
macro_rules! splitter {
( $string:expr, $sep:expr)…

mateusns12
- 25
- 2
1
vote
1 answer
How to resolve a local ambiguity error when recursively handling comma-separated tokens in macro_rules?
While trying to create a macro to ease the reading of my code, I found a "local ambiguity" in the macro. I cannot really understand how this is ambiguous though: in each arm of my bracket, the value I'm parsing is preceded by a unique…

er1t
- 59
- 5
1
vote
0 answers
How to add optional comma support in Rust?
I started learning macros in Rust. I decided to practice a little and write a simple macro for hashmap.
If call the macro this way, then everything works
let hashmap = hashmap!{ 1 => String::from("1"), 2 => String::from("2") };
But I wanted to add…

Boyar
- 11
- 1
1
vote
1 answer
Macro to generate multiple struct and trait implementations from parameters
I'm trying to create a macro using macro_rules! that would generate a series of structs along with implementations for a given trait.
A sample of what I've tried:
#[macro_export]
macro_rules! a_tree {
($name: literal, $fruit: literal) => {
…

carpdan
- 117
- 6
1
vote
1 answer
Use decl macro to form arguments to be passed to a function
I'm working on a project that uses some complex declarative macros. I ran into an issue, which I have dumbed down in the following simple code snippet. I can't understand why I cannot form the set of arguments to pass to a function with a…

pedro
- 83
- 5
1
vote
1 answer
Convert a ty to an ident in a macro_rules
I have a macro_rules that takes a function_name and calls function_name_x(). Right now I do that by passing function_name as an ident, and creating the new function name via concat_idents!.
The problem with this approach is that my IDE does not lint…

David 天宇 Wong
- 3,724
- 4
- 35
- 47
0
votes
0 answers
How do I call a function using the elements of an array as attributes
I'm new to rust and found myself tinkering with some macros. I decided, that I want to try making a macro, that can take an array and call a function, using the elements of that array as arguments for the function. This is what I have so…

Luh0
- 105
- 8
0
votes
0 answers
"Expected one of `(` or `<`, found `$`" when generating function code with a macro in Rust
To avoid writing hundreds of lines of repetitive code - I am trying to use declarative macros (macro_rules!) to generate the struct method code. For some reason though, I am getting a confusing error Expected one of '(' or '<', found '$', where it…

Dagoth
- 1
0
votes
1 answer
Rust: `pub use` / "export" many submodules at once
I need to define many types in one file (->submodule) each and expose them all on the same level of the module. This creates lots of repetitive overhead in the mod.rs:
mod foo;
mod bar;
mod baz;
[...]
pub use self::foo::*;
pub use self::bar::*;
pub…

philhob
- 3
- 3
0
votes
1 answer
Difference between metavariables within Rust macro_rules
I wrote a macro but now I am trying to extend it so I can pass it a function. I read the Rust Reference and I don't really understand metavariables. I feel like everything I want is going to be an expression, but how can I pass error to an…

dean
- 35
- 1
- 6
0
votes
1 answer
How to use unicode in a rust macro argument?
This is rougly what I want to use:
enum DashNumber {
NegInfinity,
Number(N),
Infinity,
}
macro_rules! dn {
(-∞) => {
DashNumber::NegInfinity
};
(∞) => {
DashNumber::Infinity
};
($e:expr) => {
…

Rainb
- 1,965
- 11
- 32
0
votes
2 answers
How to use & and | signs in macro_rules pattern
I'm trying to write a simple macro like this, to facilitate the writing of my code
enum Term {
Or(Box, Box),
And(Box, Box),
Value(u8),
}
macro_rules! term {
($value_1:expr & $value_2:expr) => {
…

er1t
- 59
- 5
0
votes
1 answer
macro_rules repeatition does not yield the expected token type
I am trying to match user-provided lists in a macro, but the repeat-pattern does not match the same branches depending on whether I call the macro on single items directly, or whether I call it from a repeat block.
Input code (playground…

lesurp
- 343
- 4
- 19
0
votes
1 answer
My rust macro doesn't want to accept this let statement
I've been trying to make a macro that functions similar to Python's input function.
Rather than write the stdin completely every time I wanted to automate it somewhat, and combine println! so I could kill 2 birds with one stone.
Essenitally, if…