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

What is the built-in `#[main]` attribute?

I have been using the #[tokio::main] macro in one of my programs. After importing main and using it unqualified, I encountered an unexpected error. use tokio::main; #[main] async fn main() {} error[E0659]: `main` is ambiguous --> src/main.rs:3:3 …
user18102576
13
votes
1 answer

What does an @ symbol mean in a Rust declarative macro?

I have seen the @ symbol used in macros but I cannot find mention of it in the Rust Book or in any official documentation or blog posts. For example, in this Stack Overflow answer it is used like this: macro_rules! instructions { (enum…
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
12
votes
2 answers

How do I create a proc_macro_attribute?

Now that proc_macros have been stabilized, how does one create such a thing? From what I've seen, there's the option of putting a #[proc_macro_attribute] annotation on a fn whatsitsname(attrs: TokenStream, code: TokenStream) -> TokenStream, but how…
llogiq
  • 13,815
  • 8
  • 40
  • 72
12
votes
2 answers

Creating environment for closure in a macro in Rust

I'm trying to achieve something like this (simplified): macro_rules! atest { ($closure:tt) => { let x = 5; println!("Result is {}", $closure()) }; } fn main() { //let x = 50; atest!((|| 5 + x)); } It does not work…
Ivan
  • 960
  • 7
  • 15
12
votes
1 answer

Import macro from parent module

I'm having trouble re-using macros within a crate. If a macro is defined in ./src/macros.rs: #[macro_export] macro_rules! my_macro { ... } and used in ./src/lib.rs: #[macro_use] pub mod macros; I can't see this macro in…
marcusklaas
  • 492
  • 5
  • 15
11
votes
3 answers

How to pad an array with zeros?

fn main() { let arr: [u8;8] = [97, 112, 112, 108, 101]; println!("Len is {}",arr.len()); println!("Elements are {:?}",arr); } error[E0308]: mismatched types --> src/main.rs:2:23 | 2 | let arr: [u8;8] = [97, 112, 112, 108, 101]; …
Swaroop Maddu
  • 4,289
  • 2
  • 26
  • 38
11
votes
1 answer

Why Am I Getting "Cannot Derive Macro In This Scope"?

Attempting cargo build against this code: #![allow(unused)] use serde::{Deserialize, Serialize}; use serde_json::{Result, Value}; #[derive(Serialize, Deserialize,Debug)] struct Repository{ r#type: String, url: String, } fn main() { …
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
11
votes
1 answer

Issuing a warning at compile time?

I want to issue a warning at compile time, perhaps from a macro. It should not be silenceable by cap_lints. My current use case is feature deprecation, but there's other possible uses for this.
llogiq
  • 13,815
  • 8
  • 40
  • 72
11
votes
1 answer

How to match Rust's `if` expressions in a macro?

I'm trying to write a macro that will rewrite certain Rust control flow, but I'm having difficulty matching an if expression. The problem is that the predicate is an expression, but an expr is not permitted to be followed by a block or {. The best…
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
11
votes
1 answer

Unable to use self in macro because the macro expansion ignores token `self`

I want to write a macro that prints "OK" then returns self in a method. It's my first macro, so I tried this, thinking it will just make something like a text replacement, but it fails: macro_rules! print_ok_and_return_self { () => { …
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
10
votes
2 answers

How do I create a function-like procedural macro?

How should a_proc_macro be defined so it "returns" a 5? fn main() { let a = a_proc_macro!(); assert!(a == 5); }
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
10
votes
2 answers

Is there a way to pass named arguments to format macros without repeating the variable names?

With new versions of Rust, you can simplify structure initialization like this: Foo { a: a, b: b, } to this Foo { a, b } Is it possible to do something similar for format!/println!-like macros? For now I need to write it like this: let a =…
user1244932
  • 7,352
  • 5
  • 46
  • 103
9
votes
1 answer

Cyclic package dependency while implementing proc macro

I try to implement a proc_macro Dump, which is similar to serdes Serialize. For this purpose I have a crate foo which contains my "primitive" structs (P1 and P2 in this case) which should only be dumpable. Next I do have a foo_derive crate which…
hellow
  • 12,430
  • 7
  • 56
  • 79
9
votes
2 answers

Is it possible to get the expansion of a single macro instead of the whole file?

I just found How do I see the expanded macro code that's causing my compile error?. Is it possible to get the expansion of a single macro instead of the whole file?
bertfred
  • 412
  • 4
  • 9
9
votes
2 answers

Is it possible to write something as complex as `print!` in a pure Rust macro?

I am starting out learning Rust macros, but the documentation is somewhat limited. Which is fine — they're an expert feature, I guess. While I can do basic code generation, implementation of traits, and so on, some of the built-in macros seem well…
Richard Rast
  • 1,772
  • 1
  • 14
  • 27
1
2
3
20 21