]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/procedural-macros.md
New upstream version 1.23.0+dfsg1
[rustc.git] / src / doc / reference / src / procedural-macros.md
CommitLineData
8bb4bdeb
XL
1## Procedural Macros
2
abe05a73
XL
3*Procedural macros* allow creating syntax extensions as execution of a function.
4Procedural macros can be used for is to implement custom [derive] on your own
5types. See [the book][procedural macros] for a tutorial.
8bb4bdeb
XL
6
7Procedural macros involve a few different parts of the language and its
8standard libraries. First is the `proc_macro` crate, included with Rust,
9that defines an interface for building a procedural macro. The
10`#[proc_macro_derive(Foo)]` attribute is used to mark the deriving
11function. This function must have the type signature:
12
13```rust,ignore
14use proc_macro::TokenStream;
15
16#[proc_macro_derive(Hello)]
17pub fn hello_world(input: TokenStream) -> TokenStream
18```
19
20Finally, procedural macros must be in their own crate, with the `proc-macro`
21crate type.
abe05a73
XL
22
23[derive]: attributes.html#derive
24[procedural macros]: ../book/first-edition/procedural-macros.html