Macros, Am I Rite?

2 minute read

58 Days Until I Can Walk

Still chipping away at macros here. I do have to say, procedural macros are an excellent exercise for coming to grips with Rust. I actually think I’m learning more from trying to figure out this workshop than I have from writing the ray caster.

I lost a little bit of time today to another hospital visit. All good! I finally have a physio regime, so I’ll be working on that around coding.

I made very little headway with the builder challenge today. The issue is not that I don’t know what to do. Rather I do not know what the most “Rust-like” approach is. I’m still getting kicked repeatedly by the ownership and borrowing rules, which would be fine except that I find that I am using clone a lot more than I probably should. I feel like my general approach is along the right lines, but my code could be drastically simplified if I broke things up into slightly more sensible functions.

It has also taken me most of the day to realise that the reason why I am failing unit test 6 is not because I am not correctly assigning a type to the optional current_dir field, nor is it because I am incorrectly copying data from the builder into that field. The problem is that I need to inspect the optional field and define a setter which takes the internal type as input, not an Option. It’s so obvious now that I see it, but it has taken me so long to drill down into what was going on, even when using cargo expand.

If there is a point to be made against macros, it is certainly that they can be difficult to inspect and debug. But I feel like that is well known within the Rust community, and there are tools and practices put in place to mitigate some of that challenge.

While I was waiting to be seen in the hospital, I read some of The Little Book of Rust Macros by Daniel Keep. This is an excellent book which confirmed some of my intuitions about how macros worked, but dove much deeper into the subject, by explaining where macros are expanded in the compilation process—i.e. after the AST is constructed. The book is a goldmine, and will be continuing to read it.

I’m hoping to come back to the builder with a fresher mind tomorrow morning. Today was a little all-over-the-place due to the hospital visit. Tomorrow will hopefully be more organised.

Take care folks.