// ./src/generics/phantom.md use std::marker::PhantomData; // A phantom tuple struct which is generic over `A` with hidden parameter `B`. #[derive(PartialEq)] // Allow equality test for this type. struct PhantomTuple(A, PhantomData); // A phantom type struct which is generic over `A` with hidden parameter `B`. #[derive(PartialEq)] // Allow equality test for this type. struct PhantomStruct { first: A, phantom: PhantomData } // Note: Storage is allocated for generic type `A`, but not for `B`. // Therefore, `B` cannot be used in computations. fn part0() { // Here, `f32` and `f64` are the hidden parameters. // PhantomTuple type specified as ``. let _tuple1: PhantomTuple = PhantomTuple('Q', PhantomData); // PhantomTuple type specified as ``. let _tuple2: PhantomTuple = PhantomTuple('Q', PhantomData); // Type specified as ``. let _struct1: PhantomStruct = PhantomStruct { first: 'Q', phantom: PhantomData, }; // Type specified as ``. let _struct2: PhantomStruct = PhantomStruct { first: 'Q', phantom: PhantomData, }; // Compile-time Error! Type mismatch so these cannot be compared: // println!("_tuple1 == _tuple2 yields: {}", // _tuple1 == _tuple2); // Compile-time Error! Type mismatch so these cannot be compared: // println!("_struct1 == _struct2 yields: {}", // _struct1 == _struct2); } pub fn main() { part0(); }