I want a collection of different types of structs.
A Vec
doesn't work, I think because different structs are different types, and Vec
can only contain one type.
struct Santa {
color: String,
phrase: String,
}
struct Rudolph {
speed: u32,
lumens: u32,
}
fn newSanta() -> Santa {
Santa {
color: String::from("Red"),
phrase: String::from("Ho ho ho!"),
}
}
fn newRudolph() -> Rudolph {
Rudolph {
speed: 100,
lumens: 500,
}
}
fn main() {
let santa = newSanta();
let rudolph = newRudolph();
let northerners = vec![santa, rudolph]; //fails
}
PS C:\Users\anon> rustc xmas.rs
error[E0308]: mismatched types
--> xmas.rs:27:32
|
27 | let northerners = vec![santa, rudolph]; //fails
| ^^^^^^^ expected struct `Santa`, found struct `Rudolph`
|
= note: expected type `Santa`
found type `Rudolph`