Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access struct field by variable

Tags:

rust

I want to iterate over over the fields of a struct and access its respective value for each iteration:

#[derive(Default, Debug)]
struct A {
    foo: String,
    bar: String,
    baz: String
}


fn main() {
    let fields = vec!["foo", "bar", "baz"];
    let a: A = Default::default();

    for field in fields {
        let value = a[field] // this doesn't work
    }
}

How can I access a field by variable?

like image 257
Neskews Avatar asked May 09 '26 18:05

Neskews


1 Answers

By using pattern matching, you can iterate over its fields.

#[derive(Default, Debug)]
struct A {
    foo: String,
    bar: String,
    baz: String
}

impl A {
    fn get(&self, field_string: &str) -> Result<&String, String> {
        match field_string {
            "foo" => Ok(&self.foo),
            "bar" => Ok(&self.bar),
            "baz" => Ok(&self.baz),
            _ => Err(format!("invalid field name to get '{}'", field_string))
        }
    }
}

fn main() {
    let fields = vec!["foo", "bar", "baz"];
    let a = A {
        foo: "value_of_foo".to_string(), 
        bar: "value_of_bar".to_string(), 
        baz: "value_of_baz".to_string()
    };

    for field in fields {
        let value = a.get(field).unwrap();
        println!("{:?}", value);
    }
}

returns

"value_of_foo"
"value_of_bar"
"value_of_baz"

I have written a macro that automatically implements such code for any struct. field_accessor (https://github.com/europeanplaice/field_accessor).

Cargo.toml

[dependencies]
field_accessor = "0"
use field_accessor::FieldAccessor;

#[derive(Default, Debug, FieldAccessor)]
struct A {
    foo: String,
    bar: String,
    baz: String
}

fn main() {
    let a = A {
        foo: "value_of_foo".to_string(), 
        bar: "value_of_bar".to_string(), 
        baz: "value_of_baz".to_string()
    };

    for field in a.getstructinfo().field_names.iter() {
        let value = a.get(field).unwrap();
        println!("{:?}", value);
    }
}

It also returns

"value_of_foo"
"value_of_bar"
"value_of_baz"
like image 79
Tee Avatar answered May 12 '26 11:05

Tee