rust-macros

Creating environment for closure in a macro in Rust

不羁的心 提交于 2019-11-30 03:47:46
问题 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 because the argument to the atest macro is considered by the compiler before macro evaluation: error[E0425]: cannot find value `x` in this scope --> src/main.rs:10:20 | 10 | atest!((|| 5 + x)); | ^ not found in this scope Is it possible to make this work? My understanding was that

What does an @ symbol mean in a declarative macro?

我与影子孤独终老i 提交于 2019-11-29 14:41:15
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 $ename:ident { $($vname:ident ( $($vty: ty),* )),* }) => { enum $ename { $($vname ( $($vty),* )),* } impl $ename { fn len(&self) -> usize { match self { $($ename::$vname(..) => instructions!(@count ($($vty),*))),* } } } }; (@count ()) => (0); (@count ($a:ty)) => (1); (@count ($a:ty, $b:ty)) => (2); (@count ($a:ty, $b:ty, $c:ty)) => (3); } instructions! { enum

What does the tt metavariable type mean in Rust macros?

╄→гoц情女王★ 提交于 2019-11-29 13:28:35
I'm reading a book about Rust, and start playing with Rust macros . All metavariable types are explained there and have examples, except the last one – tt . According to the book, it is a “a single token tree”. I'm curious, what is it and what is it used for? Can you please provide an example? That's a notion introduced to ensure that whatever is in a macro invocation correctly matches () , [] and {} pairs. tt will match any single token or any pair of parenthesis/brackets/braces with their content . For example, for the following program: fn main() { println!("Hello world!"); } The token

What does an @ symbol mean in a declarative macro?

家住魔仙堡 提交于 2019-11-27 07:54:17
问题 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 $ename:ident { $($vname:ident ( $($vty: ty),* )),* }) => { enum $ename { $($vname ( $($vty),* )),* } impl $ename { fn len(&self) -> usize { match self { $($ename::$vname(..) => instructions!(@count ($($vty),*))),* } } } }; (@count ()) => (0); (@count ($a:ty)) =

How do I use a macro across module files?

别来无恙 提交于 2019-11-27 03:48:55
I have two modules in separate files within the same crate, where the crate has macro_rules enabled. I want to use the macros defined in one module in another module. // macros.rs #[macro_export] // or not? is ineffectual for this, afaik macro_rules! my_macro(...) // something.rs use macros; // use macros::my_macro; <-- unresolved import (for obvious reasons) my_macro!() // <-- how? I currently hit the compiler error " macro undefined: 'my_macro' "... which makes sense; the macro system runs before the module system. How do I work around that? Lukas Kalbertodt Macros within the same crate #

Is there a way to count with macros?

独自空忆成欢 提交于 2019-11-26 12:31:15
问题 I want to create a macro that prints \"Hello\" a specified number of times. It\'s used like: many_greetings!(3); // expands to three `println!(\"Hello\");` statements The naive way to create that macro is: macro_rules! many_greetings { ($times:expr) => {{ println!(\"Hello\"); many_greetings!($times - 1); }}; (0) => (); } However, this doesn\'t work because the compiler does not evaluate expressions; $times - 1 isn\'t calculated, but fed as a new expression into the macro. 回答1: While the