4

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?

Neskews
  • 764
  • 1
  • 10
  • 23
  • 5
    This seems like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Either use some collection struct or better perhaps you can explain why exactly you need to do this – sshashank124 Jan 05 '20 at 14:16

3 Answers3

7

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"
Tee
  • 71
  • 1
  • 2
  • I'd personally stick with the common signature for get: `fn get(&self, IndexType) -> Option<&T>` instead of a `Result` with no real value. – cafce25 Jan 02 '23 at 11:56
6

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

sshashank124
  • 31,495
  • 9
  • 67
  • 76
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);
    }
}
Neskews
  • 764
  • 1
  • 10
  • 23
  • Note that you can just iterate over the values directly: `for value in B.values() {...}` without the need to create a separate `fields` or use `.iter()`/`.keys` if you also/just want the keys of the `HashMap` – cafce25 Jan 02 '23 at 11:27