Sub topic 1
Generic types and the unused parameter error
This chapter starts the tutorial with the compiler error that explains why PhantomData exists. You will see what a generic struct promises, why Rust rejects a type parameter that no field uses, and how to fix the error.
Prerequisites
This chapter assumes you already know:
- Structs and
implblocks. - Generic type parameters such as
Vec<T>andOption<T>. - How to run
cargo buildand read a compiler error.
Ownership and move semantics matter from the next chapters on, but not here.
Generic structs use their parameters
A generic struct declares a type parameter in angle brackets and uses it in a field:
struct Wrapper<T> {
value: T,
}
fn main() {
let a = Wrapper { value: 5_u32 };
let b = Wrapper { value: "hello" };
println!("{} {}", a.value, b.value);
}The compiler creates a separate type for each concrete T. Wrapper<u32> and Wrapper<&str> are different types. They have different sizes and layouts, and you cannot use one where the other is expected.
The parameter T here is used: it decides the type of value.
The unused parameter error
Suppose you want a type parameter only as a label, with no field of that type. A first attempt looks like this:
// This does not compile:
//
// struct Tagged<T> {
// raw: u64,
// }
//
// fn main() {}The compiler rejects it with error E0392:
error[E0392]: type parameter `T` is never used
--> src/main.rs:1:15
|
1 | struct Tagged<T> {
| ^ unused type parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`The help line names the fix. The error is deliberate, and the next section explains why.
Why Rust refuses it
Rust must know how a type relates to its parameters for three reasons:
- Variance. The compiler decides whether
Tagged<&'long str>can stand in forTagged<&'short str>. It works this out from how fields useT. With no field, it has nothing to analyze. - Drop check. The compiler needs to know whether dropping a value might drop a
T. This affects what borrows are allowed to dangle. - Auto traits.
SendandSyncare derived from field types. An unusedTgives no information about thread safety.
Rather than guess, Rust makes you state the relationship.
The fix: PhantomData
std::marker::PhantomData<T> is a type with exactly one value, also written PhantomData. It holds no data. Adding a field of this type tells the compiler "this struct behaves as if it relates to T":
use std::marker::PhantomData;
struct Tagged<T> {
raw: u64,
_marker: PhantomData<T>,
}
struct Meters;
fn main() {
let a: Tagged<Meters> = Tagged {
raw: 5,
_marker: PhantomData,
};
println!("{}", a.raw);
println!("{}", std::mem::size_of::<Tagged<Meters>>());
}The program prints 5 and then 8. The struct is exactly the size of its u64, so the tag costs nothing at runtime. The compiler warns that Meters is never constructed. You can ignore the warning, because Meters exists only to be named as a type.
The field name starts with an underscore so the compiler does not warn that the field is never read.
What you have now
Tagged<Meters> and Tagged<Seconds> are distinct types with identical layouts. The compiler will refuse to mix them, and no data distinguishes them at runtime. The rest of the tutorial builds on this.
First, the next chapter looks at zero-sized types, which explain why both PhantomData and labels like Meters take no space.
Exercise
Why does struct Label<T> { id: u32 } fail to compile, and what is the smallest change that fixes it without removing T?
Show answer
No field uses T, so the compiler reports E0392 because it cannot work out variance, drop check, or auto-trait behavior for the parameter. Add a field _marker: PhantomData<T> (importing std::marker::PhantomData) and construct values with _marker: PhantomData.