'Access struct field by variable

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?



Solution 1:[1]

Rust doesn't have any way of iterating directly over its fields. You should instead use a collection type such as Vec, array or one of the collections in std::collections if your data semantically represents a collection of some sort.

If you still feel the need to iterate over the fields, perhaps you need to re-consider your approach to your task and see if there isn't a more idiomatic/proper way to accomplish it

Solution 2:[2]

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 am now writing a macro that implements such codes automatically for any struct, although there may be some bugs. 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"

Solution 3:[3]

Based on the answer of sshashank124 I came to the conclusion that I should use an Hashmap instead of a struct:

fn main() {
    let mut B = HashMap::new();
    B.insert("foo", 1);
    B.insert("bar", 2);
    B.insert("baz", 3);

    let fields = vec!["foo", "bar", "baz"];

    for &field in &fields {
        let value = B.get(field);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sshashank124
Solution 2 Tee
Solution 3 Neskews