A phantom type lets a Rust struct carry type information without storing a value of that type. Use it when the distinction matters at compile time, such as tagging a dog with its breed without adding breed data to the struct. section 2.1.5
use std::marker::PhantomData;
struct Dog<Breed> {
name: String,
breed: PhantomData<Breed>,
}
Add a PhantomData field to use the generic parameter and satisfy the compiler. Construction still requires a PhantomData value; the marker is optimized out at compile time. 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"
}
}
Distinct breed types allow separate Dog implementations, so each type can return its breed name without storing that name as state. section 2.1.5

