Enumを使うとベクタに複数の型を保持できる
2022/2/19 13:50:00
fn main() {
enum SpreadsheetCell {
Int(i32),
Float(f64),
Text(String),
}
let row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Text(String::from("blue")),
SpreadsheetCell::Float(10.12),
];
for r in &row {
match r {
SpreadsheetCell::Int(i) => println!("{}", i),
SpreadsheetCell::Text(s) => println!("{}", s),
SpreadsheetCell::Float(f) => println!("{}", f),
}
}
}
3
blue
10.12
https://doc.rust-jp.rs/book-ja/ch08-01-vectors.html#enumを使って複数の型を保持する