Automatically generate Rust code on compilation

⚓ rust    📅 2025-05-19    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 42 days ago. The information described in this article may have changed.

Backstory: I am porting some C# library to Rust. The library uses some T4 script to generate C# code compile-time to be included in build, by using another C# code. The code itself is reading several files from build environment and populates several non-trivial classes with read data and their relationship.

Is something like that possible in Rust? So far I reckon:

  • One can use build.rs to execute Rust code compile-time, but I wonder if outputing a new Rust source from read data in this step is a good strategy
  • I can use some macro magic. Honestely I worked very little with Rust macros and I would welcome a small proof-of-concept example of how can I read file inside a macro, execute some Rust code and output result as Rust code

2 posts - 2 participants

Read full topic

🏷️ rust_feed