# Fixing proc\_macro example

**URL:** <https://users.rust-lang.org/t/fixing-proc-macro-example/24711>\
**Category:** uncategorized\
**Created:** [January 28, 2019, 4:52pm UTC](https://users.rust-lang.org/t/fixing-proc-macro-example/24711 "2019-01-28T16:52:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon80458984](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@anon80458984](https://users.rust-lang.org/u/anon80458984)\
**Post date:** [January 28, 2019, 4:52pm UTC](https://users.rust-lang.org/t/fixing-proc-macro-example/24711/1 "2019-01-28T16:52:03Z")

</div>

1. Sample code:

```rust
#![crate_type = "proc-macro"]
#![feature(extern_crate_item_prelude)]
extern crate proc_macro;
extern crate syn;

#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
#[proc_macro_derive(TypeName)]

pub fn type_name(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_typename(&ast).into()
}

fn impl_typename(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = &ast.ident;
    quote! {
      impl TypeName for #name {
        fn typename() -> String {
          stringify!(#name).to_string()
        }
      }
    }
}

```

1. The above code is from a Rust book I am reading. I am not familiar with procedural macros, and am not sure if (1) I'm doing something wrong or (2) if the code is outdated. Either way, pointing out what's wrong with the code / how to fix it would be helpful.

2. I am familiar with Rust macro-rules. I am also familiar with Lisp style macros, so the `quote!` looks like lisp 'quasi-quote' and the `#` looks like Lisp unquote.

---

<div class="post-metadata">

**Author:** ![anon80458984](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@anon80458984](https://users.rust-lang.org/u/anon80458984)\
**Post date:** [January 28, 2019, 4:53pm UTC](https://users.rust-lang.org/t/fixing-proc-macro-example/24711/2 "2019-01-28T16:53:15Z")

</div>

TO be clear: I don't have any questions about the concept of macros -- i.e. AST -\> AST transforms.

It's the "low level mechanics" of getting the macro to compile that I am confused about.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [January 28, 2019, 5:38pm UTC](https://users.rust-lang.org/t/fixing-proc-macro-example/24711/3 "2019-01-28T17:38:51Z")

</div>

Try this

```rust
/* in Cargo.toml

[lib]
proc-macro = true

[dependencies]
syn = '0.15.26'
quote = '0.6.11'

 */

extern crate proc_macro;
extern crate syn;

#[macro_use]
extern crate quote;
use proc_macro::TokenStream;

#[proc_macro_derive(TypeName)]
pub fn type_name(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_typename(&ast).into()
}

fn impl_typename(ast: &syn::DeriveInput) -> impl Into<TokenStream> {
    let name = &ast.ident;
    quote! {
      impl TypeName for #name {
        fn typename() -> String {
          stringify!(#name).to_string()
        }
      }
    }
}

```

* * *

There seems to be some inconsistency between `quote` and `syn`, I don't know why.

---

<div class="post-metadata">

**Author:** ![anon80458984](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@anon80458984](https://users.rust-lang.org/u/anon80458984)\
**Post date:** [January 28, 2019, 6:08pm UTC](https://users.rust-lang.org/t/fixing-proc-macro-example/24711/4 "2019-01-28T18:08:40Z")

</div>

> [@RustyYato](#):
>
> fn impl\_typename(ast: &syn::DeriveInput) -\> impl Into\<TokenStream\> {

Adding the `impl Into<...>` fixed everything. Thanks!

---

<div class="post-metadata">

**Author:** ![dtolnay](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dtolnay/32/2115_2.png) [@dtolnay](https://users.rust-lang.org/u/dtolnay)\
**Post date:** [January 28, 2019, 7:06pm UTC](https://users.rust-lang.org/t/fixing-proc-macro-example/24711/5 "2019-01-28T19:06:28Z")

</div>

The code in the book is outdated.

You can find up-to-date [syn documentation here](https://github.com/dtolnay/syn) and [quote documentation here](https://github.com/dtolnay/quote).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:50am UTC](https://users.rust-lang.org/t/fixing-proc-macro-example/24711/6 "2023-01-12T08:50:11Z")

</div>


