Skip to content

Commit f8ca506

Browse files
committed
Move proc_macro example to the intro and into an example block
1 parent 26054e2 commit f8ca506

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

src/procedural-macros.md

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,39 @@ These macros are defined by a [public] [function] with the `proc_macro` [att
9292
r[macro.proc.function.namespace]
9393
The `proc_macro` attribute defines the macro in the [macro namespace] in the root of the crate.
9494
95-
For example, the following macro definition ignores its input and outputs a function `answer` into its scope.
96-
97-
<!-- ignore: test doesn't support proc-macro -->
98-
```rust,ignore
99-
# #![crate_type = "proc-macro"]
100-
extern crate proc_macro;
101-
use proc_macro::TokenStream;
95+
> [!EXAMPLE]
96+
> The following macro definition ignores its input and outputs a function `answer` into its scope.
97+
>
98+
> <!-- ignore: test doesn't support proc-macro -->
99+
> ```rust,ignore
100+
> # #![crate_type = "proc-macro"]
101+
> extern crate proc_macro;
102+
> use proc_macro::TokenStream;
103+
>
104+
> #[proc_macro]
105+
> pub fn make_answer(_item: TokenStream) -> TokenStream {
106+
> "fn answer() -> u32 { 42 }".parse().unwrap()
107+
> }
108+
> ```
109+
>
110+
> And then we use it in a binary crate to print "42" to standard output.
111+
>
112+
> <!-- ignore: requires external crates -->
113+
> ```rust,ignore
114+
> extern crate proc_macro_examples;
115+
> use proc_macro_examples::make_answer;
116+
>
117+
> make_answer!();
118+
>
119+
> fn main() {
120+
> println!("{}", answer());
121+
> }
122+
> ```
102123
103-
#[proc_macro]
104-
pub fn make_answer(_item: TokenStream) -> TokenStream {
105-
"fn answer() -> u32 { 42 }".parse().unwrap()
106-
}
107-
```
108124
109-
And then we use it in a binary crate to print "42" to standard output.
110125
111-
<!-- ignore: requires external crates -->
112-
```rust,ignore
113-
extern crate proc_macro_examples;
114-
use proc_macro_examples::make_answer;
115126
116-
make_answer!();
117127
118-
fn main() {
119-
println!("{}", answer());
120-
}
121-
```
122128
123129
r[macro.proc.function.invocation]
124130
Function-like procedural macros may be invoked in any macro invocation position, which includes [statements], [expressions], [patterns], [type expressions], [item] positions, including items in [`extern` blocks], inherent and trait [implementations], and [trait definitions].

0 commit comments

Comments
 (0)