Get declared variable name with syn macro


I am trying to get variable declarations in procedural macro, but couldn't find in docs how to do it correctly.
Please, help me.

let fn_item: ItemFn;
fn_item.block.stmts.clone().into_iter().for_each(|stmt| {
        if let syn::Stmt::Local(local) = stmt {
            // get declared variable name

The tokens after let aren't limited to just being an identifier, they can also be a pattern.

For example you can destructure a tuple

let (x, y) = (1, 2);

So syn has to handle all those cases too. The Pat enum is responsible for representing them all. If you only care about the simple case you can match on the Ident case and error on the others.

1 Like

Thank you :hearts: This helped me a lot!