Skip to content

Commit 496be3a

Browse files
committed
Try out a different example
1 parent 6260967 commit 496be3a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

posts/2018-12-21-Procedural-Macros-in-Rust-2018.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ For example, a small macro like:
266266

267267
```rust
268268
#[proc_macro]
269-
pub fn gen_fn(expr: TokenStream) -> TokenStream {
269+
pub fn make_pub(item: TokenStream) -> TokenStream {
270270
let result = quote! {
271-
fn the_function() -> u32 { #expr }
271+
pub #item
272272
};
273273
result.into()
274274
}
@@ -277,24 +277,26 @@ pub fn gen_fn(expr: TokenStream) -> TokenStream {
277277
when invoked as:
278278

279279
```rust
280-
my_macro::gen_fn!("foo");
280+
my_macro::make_pub! {
281+
static X: u32 = "foo";
282+
}
281283
```
282284

283285
is invalid because we're returning a string from a function that should return a
284286
`u32`, and the compiler will helpfully diagnose the problem as:
285287

286288
```
287289
error[E0308]: mismatched types
288-
--> src/main.rs:1:19
290+
--> src/main.rs:1:37
289291
|
290-
1 | my_macro::gen_fn!("foo");
291-
| ------------------^^^^^--
292-
| | |
293-
| | expected u32, found reference
294-
| expected `u32` because of return type
292+
1 | my_macro::make_pub!(static X: u32 = "foo");
293+
| ^^^^^ expected u32, found reference
295294
|
296295
= note: expected type `u32`
297296
found type `&'static str`
297+
298+
error: aborting due to previous error
299+
298300
```
299301

300302
And we can see here that although we're generating brand new syntax, the

0 commit comments

Comments
 (0)