For now, this reference is a best-effort document. We strive for validity and completeness, but are not yet there. In the future, the docs and lang teams will work together to figure out how best to do this. Until then, this is a best-effort attempt. If you find something wrong or missing, file an issue or send in a pull request.

Procedural Macros

Procedural macros allow creating syntax extensions as execution of a function. Procedural macros can be used to implement custom derive on your own types. See the book for a tutorial.

Procedural macros involve a few different parts of the language and its standard libraries. First is the proc_macro crate, included with Rust, that defines an interface for building a procedural macro. The #[proc_macro_derive(Foo)] attribute is used to mark the deriving function. This function must have the type signature:

use proc_macro::TokenStream;

pub fn hello_world(input: TokenStream) -> TokenStream

Finally, procedural macros must be in their own crate, with the proc-macro crate type.