O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The minimal compiler plugin

Compiler plugins are built as separate crates and they include a plugin registration function. The crate is then linked into the main application in a special way. Here's a compiler plugin that adds a new macro. The macro, when called in an application, prints a greeting at compile time:

// simplest-compiler-plugin.rs #![feature(plugin_registrar, rustc_private)] extern crate syntax; extern crate rustc_plugin; use syntax::tokenstream::TokenTree; use syntax::ext::base::{ExtCtxt, DummyResult, MacResult}; use syntax::ext::quote::rt::Span; use rustc_plugin::Registry; fn hello(_: &mut ExtCtxt, sp: Span, _: &[TokenTree])          -> Box<MacResult + 'static> {     println!("Hello!");     DummyResult::any(sp) } #[plugin_registrar]  ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required