Can you create structs programmatically?

Specifically, I have a program that reads data from two user-provided .csv files and I'm wondering if it's possible to write code that creates a struct based on column names in the files. Is that doable or must structs be defined pre-compilation?

It's certainly possible to generate code during compilation based on some external input (not very simple, but possible). If the .csv is available only at runtime, your best route might be of simply using HashMap.


must structs be defined pre-compilation?

Yes, Rust is a statically typed language, so every type must be defined at compile time. That being said, as mentioned by @Cerber-Ursi, if your CSV files are available at compile time, then you can for instance use a script to generate a Rust file containing the definitions of the associated types before the program is compiled.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.