0
votes

Assume we have a Person struct and a PersonName struct that used by a field, like the following:

struct Person {
    name: PersonName,
    age: u8,
}

struct PersonName {
    name: String,
    middle_name: Option<String>,
    surname: Option<String>,
}

Create the struct:

let foo = Person { name: PersonName{name: String::from("bar"), middle_name: None, surname: String::from("baz") }, age: 16u8 };

How can I use this string to gain access to the struct field specified in it?

let foo_surname = eval("name.surname") // returns "baz"
// struct evaluator returns the value of specific field
// i.e.: field_path: "my_struct_field.sub_field1.sub_sub_field_1"
fn eval(field_path: String) -> ...{}

Is there any way to do this without writing a custom evaluator?

1
You can do this by creating you own trait and create you own derive macro for this trait. But it is VERY complicated to be described here. - Dmitry

1 Answers

1
votes

There is no way to do this. Rust is a statically compiled language where this kind of information doesn't existing anymore during the program runtime.