Reputation: 417
struct Test;
// here must be code to overload the standard field resolution methods
fn main() {
let t = Test;
println!("I wanna catch request for unknown struct field {}", t.unexpected_field)
}
Upvotes: 2
Views: 368
Reputation: 432109
No. Rust is a statically compiled language; everything must be resolved at compile time. Either a field exists or it does not.
Even if the field did conditionally exist, you wouldn't know what type it was so you wouldn't know what methods exist on it. The compiler wouldn't know what size the field is so it couldn't allocate space for the structure to begin with.
It's more likely that you want to use Option
:
struct Test {
unexpected_field: Option<i32>,
}
Or you could switch to a HashMap
.
Editorially, this is a huge reason to have a statically-compiled language and it's a great thing.
Upvotes: 5