3.2. Procedural Macros

Procedural Macros

"Procedural macros" are the second way to implement a macro. For now, the only thing they can be used for is to implement 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;

#[proc_macro_derive(Hello)]
pub fn hello_world(input: TokenStream) -> TokenStream

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

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/reference/procedural-macros.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部