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.

8 Likes

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 @Cerberuser, if your CSV files are available at compile time, then you can for instance use a build.rs script to generate a Rust file containing the definitions of the associated types before the program is compiled.

2 Likes