- Slow down :-)
- Write the code by hand first (in a regular *.rs file)
- make it compile & make tests pass
- compiler error messages are helpful and point to correct lines
- extract & generalize code block into the macro
- use
cargo clean
when you see unexpected behavior - use
cargo expand <module>
to bring broken code back into regular rs file- ... so the compiler can help you
- Horrible support in Jetbrains products
- When derive proc macro fails, compiler doesn't show failing line
-Zmacro-backtrace
does NOT help
- Jetbrains Debugger doesn't work inside proc macro code
- Error messages won't tell you which line in the macro is broken
- worst case: use
panic!("{:?}", x)
to print things
- Expands attributes on structs, fields, etc
- Works even when code doesn't compile
cargo install cargo-expand
- put example code in one rs file (eg.
tests/example.rs
) - put test module (with assertions) in separate file (eg.
tests/example_tests.rs
)
cd $DIR_WITH_CARGO_TOML
TEST_RS_FILE_WITHOUT_EXTENSION=my_test
cargo expand --test $TEST_RS_FILE_WITHOUT_EXTENSION --color=always --theme=Dracula --tests
- Write the code by hand (eg. the
impl
) - fix all compiler errors, then bring result back to the macro (and generalize)