I'm just learning Rust. So I know this works:
enum Animal {
Cat { name: String, weight: f64 }
}
fn main() {
let a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
match a {
Animal::Cat{name, weight} => { println!("Cat n={} w={}", name, weight); }
}
}
but why can I not assign from an enum field directly like this:
...
let wt = a.weight;
Or am I using the wrong syntax? Or is it because Rust cannot guarantee an Animal instance will be of type Cat?
Is the only way to access fields of an instance of enum struct or tuple variant by using match?