I'm new to rust and am trying to figure out if this is possible.
So sometimes it's cleaner for functions/methods to access data as an array, and sometimes it's cleaner to access data by name
in rust I can define something like this:
struct Vector3D{
x : f64,
y : f64,
z : f64,
coordinates : [f64;3]
}
But there needs to only be a single place where each value is stored.
In Java what I'm asking for is (I believe) not possible and the solution I use is this:
public class Vector3D {
public static class Vec3D {
public static final int ARRAY_SIZE = 3;
public static final int X = 0;
public static final int Y = 1;
public static final int Z = 2;
}
private double[] coords;
public double x(){
return coords[Vec3D.X];
}
}
which is a bit kludgy, but allows me to access the values of the fixed size arrays by name.
Is there a better way to do something like this in Rust?