logoalt Hacker News

ketzo11/08/20242 repliesview on HN

So how do you debug code written with macros like this, or come into it as a new user of the codebase?

I’m imagining seeing the node! macro used, and seeing the macro definition, but still having a tough time knowing exactly what code is produced.

Do I just use the Example and see what type hints I get from it? Can I hover over it in my IDE and see an expanded version? Do I need to reference the compiled code to be sure?

(I do all my work in JS/TS so I don’t touch any macros; just curious about the workflow here!)


Replies

schneems11/08/2024

Run:

    $ cargo expand
And you’ll see the resulting code.

Rust is really several languages, ”vanilla” rust, declarative macros and proc macros. Each have a slightly different capability set and different dialect. You get used to working with each in turn over time.

Also unit tests is generally a good playground area to understand the impacts of modifying a macro.

guitarbill11/08/2024

rust-analyzer, the Rust LSP used in e.g. VSCode, can expand declarative and proc macros recursively.

it isn't too bad, although the fewer proc macros in a code base, the better. declarative macros are slightly easier to grok, but much easier to maintain and test. (i feel the same way about opaque codegen in other languages.)