3

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`
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Hydraxan14
  • 618
  • 2
  • 6
  • 18

1 Answers1

5

The usual way would be to create an enum with a variant for each kind of struct. When you process them, you'll need to match on their enum variant to determine what their type is.

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,
    }
}

enum Common {
  Santa(Santa),
  Rudolph(Rudolph),
}

fn main() {
    let santa = newSanta();
    let rudolph = newRudolph();

    let northerners = vec![Common::Santa(santa), Common::Rudolph(rudolph)];

    match &northerners[0] {
        Common::Santa(santa) => println!("santa color: {}", santa.color),
        Common::Rudolph(rudolph) => println!("rudolph speed: {}", rudolph.speed),
    }
}

playground

Alternatively, if they each implement a common trait, you can store trait objects of them.

Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123