Macro-Roni? AI-yi-yi!
Tags: beginners, programming, rust, codenewbie
Published: 2023-02-08 21:16:11


<h3>Old Tutorials, ChatGPT, and No Mentor: How I Failed to Build a Rust Procedural Macro That Worked. (Until One Finally Did.)</h3>

***Have you*** heard the old saying "Never get ahead of your skis"? Or your skates? Or your common sense?

Recently, I got tired of being a post-"Hello, world" Rust newbie, especially one who has been plodding forward at a turtle-like, spare-time pace. I've been trying to become competent at Rust by using books, online tutorials, and no in-person guidance, one code example at a time.

**Rust is** not an easy language to learn, especially *that* way. And, unfortunately, I still work remotely and must--for survival's sake--minimize my exposure to a wide range of illnesses. That sums up my basic path forward.

In a burst of impatience, however, I decided to vault ahead and tackle something more substantial than, say, shadowing or immutability, in (another old saying) one fell swoop. After all, I had successfully done some remote work not long ago that required a few basic Rust skills. Whatever I jumped into next involving Rust, I could figure out on the fly, I reasoned. *Nope!*

**Out of the digital blue**, and without fully comprehending what the term meant, I decided I would teach myself how to create a Rust procedural macro. *Easy, peasy*, I reasoned. What could be hard about making some macro-*roni*? 

Speaking of fell swoops and getting ahead of one's skis, let's note my photograph (above) of the failing, falling pole vaulter and move on to some other sports imagery. When I swooped off the end of Rust's macro ski jump on my first attempt and briefly stood on thin air like the cartoon character Wile E. Coyote...I realized I had left both skis behind, neatly parked at the top of the run. The hard landing that followed completely broke my common sense. Indeed, I ran back up the macro mountain to try again.

**I re-consulted** a Rust how-to book for more information. The book described the alleged wonders of procedural macros but gave no complete code examples. I found some aging procedural macro tutorials online and carefully copied their code. Cargo test? Fail. Cargo build? Fail. Some of them tried to compile, then flashed some error messages. Cargo run? Exactly none.

Over the next two days, I tried to run and rework the code from several tutorial examples. Always, there were frustrating holes in the how-to descriptions. And clear assumptions had been made that those attempting the tutorials would be smarter and more experienced at Rust than I was. My code "fixes" only made matters worse.     

**Finally, completely** flummoxed by failing to compile even a "Hello, world" procedural macro, I decided to cheat and get help from AI. I asked ChatGPT to show me the code and describe all steps necessary to build a simple, working example.

It took a few minutes to get a response from the wildly popular, overloaded site. But I eagerly copied the arriving code and followed each calm, confident step in the AI-generated tutorial. The secrets of procedural macros were about to be revealed at last, I believed.

**Cargo build?** Nope. Fail. And this time, I spotted a startling code typo. The "all knowing" AI had told me to use proc-macro in a place where proc_macro clearly was correct. Fixing that, of course, still did nothing to solve my dilemma. Cryptic new error messages appeared.

Defeated at last, I deleted the entire procedural macro saga from my hard drive and vowed to quit Rust. Perhaps I could go home to Python, I reasoned. Or take up C# again and maybe build some model airplanes on the side. 

**Breaking news!** After completely giving up Rust *forever* for a few hours, I came back for one more round of macro boxing. This time, I ducked and weaved--and noodled around on the web a bit more. And I came across something I should have paid much closer attention to many days ago: [The Rust Programming Language](https://doc.rust-lang.org/book/) book. Very close to the end of its huge piles of pages, it offers some [procedural macro examples](https://doc.rust-lang.org/book/ch19-06-macros.html).

I chose "How to Write a Custom derive Macro." And once again, I ran into descriptions that seemed short on a few essential details. I kept doing the code *as shown*, and Cargo build kept failing. Finally, almost by accident, I clicked on a screen prompt I had not noticed before: The hidden-lines link in Listing 19-33. The hidden lines now popped into view, and I quickly saw where and why my coding was going off the rails. Copy, paste, Cargo build, Cargo run...for the *win*! 

![All of this...just for Pancakes???] (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1idji6qg2e0tfpfz6et0.png)

**What's the moral** of this story? For me, it's this: I need to reach out more often from my pandemic-walled-off, remote-work castle and humbly ask for help from people who don't mind showing a newbie how to do something that's over his or her head. Trying to learn Rust in a vacuum is a sure-fire way to quickly run out of air. Other newbies should remember this, too, I believe.

Also, I need to go straight back to the Rust playground, stay in line, keep learning patiently, and get much better at the basics first. I've been to the procedural macro mountaintop, yes, and discovered I was almost completely clueless about what I could do once that I was there.

**When I do** return, please do me one small favor in honor of the procedural macro that I finally got to work. *Don't* call me "Pancakes."

###### Cover image credit: _Photo by Si Dunn_