liveBook · Manning

Syn field

As explained in Write Powerful Rust Macros

Field is a syn syntax structure representing a Rust struct or enum field and its properties.

Field is the syn syntax structure used to inspect a Rust field while traversing a parsed struct. Its properties include attributes, visibility, identifier, and type. section 4.4

Named struct fields appear as a Punctuated<Field, Token![,]> collection. Iterating over it yields each Field while ignoring the comma punctuation. section 4.4

Generating public fields

A procedural attribute macro matches a struct's named fields, reads each field's ident and ty, and emits quote! { pub #name: #ty } for the generated version.

How Write Powerful Rust Macros covers it

How deep it goes
Traces fields from DeriveInput through DataStruct and FieldsNamed, then iterates them and reads identifiers and types.
Examples
Demonstrates matching named fields and generating public declarations from each field's identifier and type.
What you can do after
Build it from scratch by traversing named fields and generating corresponding declarations with syn and quote.
How it connects
Connects Field with syn, DeriveInput, DataStruct, FieldsNamed, Punctuated, Token, and quote.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.