PhantomData is a marker type, it is zero-sized type, we can use it to indicate that a type parameter is used without actually carry a value of that type.
use std::marker::PhantomData;
fn main() {
let ms = MyStruct {f1: 10, f2: PhantomData};
println!("{:?}", ms)
}
#[derive(Debug)]
struct MyStruct<T> {
f1: T,
f2: PhantomData<T>
}
We create a struct MyStruct and add a generic type parameter T, we add 2 fields ,f1 and f2, we specify f1 field’s type is T, f2 filed’s type is PhantomData<T>. We add attribute derive(Debug), so we can print the instance of the struct .
In main function we create an instance of MyStruct ,f1 field we give it i32 value 10 , f2 filed we can give it a value PhantomData which is zero-sized value.
Last we print out this instance.