How can I get value of first element of enum vector?

let row = vec![
    SpreadsheetCell::Int(3),
    SpreadsheetCell::Text(String::from("blue")),
    SpreadsheetCell::Float(10.12),
];

how can I get the value of first element (I want the 3 value in SpreadsheetCell::Int(3))?

let at_first: &SpreadsheetCell = &row[0]; // panics if out of bounds
let three = match at_first {
    | &SpreadsheetCell::Int(value) => value,
    | _ => {
        // handle case where there isn't an `Int`
        unreachable!() // if you are sure of it
    },
};
assert_eq!(three, 3);

which can be shortened to:

let three =
    if let &SpreadsheetCell::Int(it) = &row[0] { it } else { unreachable!() }
;
assert_eq!(three, 3);
3 Likes