In my program, I represent a history of changes to fields of some objects as follows:
struct FieldChange {
property_name: &'static str,
// some other fields
}
let history = Vec<FieldChange>::new();
I handle it like this:
match field_change.property_name {
"name" => // do something,
"age" => // do something,
_ => {}
}
To improve with readability and ease future refactoring, I would like to write something like this:
match field_change.property_name {
nameof(Person::name) => // do something,
nameof(Person::age) => // do something,
_ => {}
}
where nameof
generates a &str
representation of the field, similar to nameof
in C#.
The main point I'm looking for, is that the compiler can verify whether the fields exist (e.g., in this case, whether Person
indeed has name
and age
fields). Is it possible to extract the field-names like this in Rust?
Aucun commentaire:
Enregistrer un commentaire