Marker structs let a generic type carry compile-time information without storing that information as state. In Rust, `PhantomData` marks an otherwise unused generic parameter; empty types can then distinguish concrete specializations. section 2.1.5
use std::marker::PhantomData;
struct Dog<Breed> {
name: String,
breed: PhantomData<Breed>,
}
Add `PhantomData<Breed>` so the compiler accepts the generic parameter without storing a breed value. section 2.1.5
impl Dog<Labrador> { #1
fn breed_name(&self) -> &str {
"labrador" #2
}
}
impl Dog<Retriever> {
fn breed_name(&self) -> &str {
"retriever"
}
}
impl Dog<Poodle> {
fn breed_name(&self) -> &str {
"poodle"
}
}
impl Dog<Dachshund> {
fn breed_name(&self) -> &str {
"dachshund"
}
}
Concrete implementations specialize `Dog` for each marker type and provide breed-specific methods. section 2.1.5
