Basically, I want to specify the fields. I want to do something using a vector of some kind.
Not sure how to specify the fields in the vector
I am not sure how to use the fields in the vector. to get to the struct fields.
In Javascript, I would be able to place the fields as strings in an array ["person_a"]. then iterate the array, then call say_hello(bus[field]);
fn main() {
let bus = Bus{
person_a:70,
person_b:56,
person_c:35,
};
// 1. let v = ["person_a","person_c"];
// 2. then apply a function to only those fields
}
struct Bus{
person_a:i64,
person_b:i64,
person_c:i64
}
fn say_hello(field:i64){
println!("hello {}",field);
}
Indeed. This works because Javascript doesn't have struct types. What is does have is language-provided hash map. And, of course, if you would use HashMap instead of struct in Rust what you want would be trivial.
I don't recommend you to do this, though: because Javascript doesn't have struct types on language level Javascript JIT-compilers do a lot of work to recognize a situation where hash map is used like a struct and try to convert hash map into struct when that makes some sense. With transparent fallback back to full-blown hash map if you pick the other code path.
Rust assumes that if you need a struct you would, indeed, use struct. What's the “business task” you are trying to solve here?
Actually I suspect HashMap<&'static str, i32> is exactly what OP want, given field name student_{a, b, c}. OP is more likely confused what Rust struct means and used wrong tool.