Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of Rust plugins/syntax extensions can be written and where are they documented?

Tags:

rust

This site goes over six types of syntax extensions in Rust as of 1.0. Decorator, Modifier, MultiModifier, NormalTT, IdentTT, and MacroRulesTT.

Unfortunately, all of its links to documentation for these (and related compiler extension) items appear to be defunct. Indeed, even manually searching the Rust documentation for SyntaxExtension or even the syntax module yields nothing. It still appears to be in the main Rust repository in the master branch, so it's clearly not a matter of just being in the nursery somewhere.

Of note, that page mentions that at some point "soon" (as of 1.0) MultiModifier and Modifier may be merged, but due to the disappearing documentation I can't confirm this as of the current Nightly.

The book only documents basic lints and "procedural macros", which seem to be fairly limited in scope.

So, as of now, are there still 6 syntax extension types? Has their documentation been kept up to date in some new location? Do each of them still perform roughly the same function as they did around 1.0?

like image 514
Linear Avatar asked Oct 26 '16 07:10

Linear


1 Answers

That site is pretty out of date — it's from before Rust was stabilized. The only "plugins/syntax extensions" supported in Rust 1.20 are macros by example and custom derive, both of which are in The Rust Programming Language. Unstable Rust also has custom attributes and procedural macros, but both of those will likely change before they are stabilized.

A rough mapping of the extension types listed in that site to the current types of macros would be something like:

  • Decorator -> Custom Derive
  • Modifier, MultiModifier -> Custom Attribute
  • NormalTT -> Procedural Macros
  • IdentTT -> Procedural Macros (I don't entirely understand what they are though, so I'm not sure)
  • MacroRulesTT -> Macros by Example (a.k.a. Declarative Macros)

Examples of macros by example are easy to find (for example std and the log crate make extensive use of them). The serde_derive crate is probably the best example of using custom derive.

UPDATE:

Procederual macros (including custom attributes) were stabilized in Rust 1.30. See https://doc.rust-lang.org/stable/book/ch19-06-macros.html

like image 188
Thayne Avatar answered Oct 24 '22 22:10

Thayne