-
Create a new sub-crate for the custom derive with cargo new chapter-five-derive.
- Open the newly generated chapter-five-derive/Cargo.toml.
- Add this directly above the [dependencies] section of the file in order to mark the crate as a procedural macro crate:
[lib]
proc-macro = true
- Under [dependencies], add the following lines:
syn = "0.11.11"
quote = "0.3.15"
If you want, you can go to the crates.io web pages for syn (https://crates.io/crates/syn) and quote (https://crates.io/crates/quote) to check for the newest version and use that one instead.
- In the chapter-five-derive/src/lib.rs file, delete the generated code and add the following:
1 extern crate proc_macro;
2 #[macro_use]
3 extern crate quote;
4 extern crate syn;
5
6 use proc_macro::TokenStream;
7
8 // HelloWorld is the name for the derive
9 // hello_world_name is the name of our optional attribute
10 #[proc_macro_derive(HelloWorld, attributes(hello_world_name...