This content originally appeared on DEV Community and was authored by Labby
Introduction
This article covers the following tech skills:
In this lab, we explore the concept of phantom type parameters, which are type parameters that are checked statically at compile time and do not have any runtime behavior or values. We demonstrate their usage in Rust by combining std::marker::PhantomData
with the concept of phantom type parameters to create tuples and structs that contain different data types.
Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use
main.rs
, compile and run it withrustc main.rs && ./main
.
Phantom type parameters
A phantom type parameter is one that doesn't show up at runtime, but is checked statically (and only) at compile time.
Data types can use extra generic type parameters to act as markers or to perform type checking at compile time. These extra parameters hold no storage values, and have no runtime behavior.
In the following example, we combine [std::marker::PhantomData] with the phantom type parameter concept to create tuples containing different data types.
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, B>(A, PhantomData<B>);
// A phantom type struct which is generic over `A` with hidden parameter `B`.
#[derive(PartialEq)] // Allow equality test for this type.
struct PhantomStruct<A, B> { first: A, phantom: PhantomData<B> }
// Note: Storage is allocated for generic type `A`, but not for `B`.
// Therefore, `B` cannot be used in computations.
fn main() {
// Here, `f32` and `f64` are the hidden parameters.
// PhantomTuple type specified as `<char, f32>`.
let _tuple1: PhantomTuple<char, f32> = PhantomTuple('Q', PhantomData);
// PhantomTuple type specified as `<char, f64>`.
let _tuple2: PhantomTuple<char, f64> = PhantomTuple('Q', PhantomData);
// Type specified as `<char, f32>`.
let _struct1: PhantomStruct<char, f32> = PhantomStruct {
first: 'Q',
phantom: PhantomData,
};
// Type specified as `<char, f64>`.
let _struct2: PhantomStruct<char, f64> = 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);
}
Summary
Congratulations! You have completed the Phantom Type Parameters lab. You can practice more labs in LabEx to improve your skills.
🚀 Practice Now: Phantom Type Parameters
Want to Learn More?
- 🌳 Learn the latest Rust Skill Trees
- đź“– Read More Rust Tutorials
- đź’¬ Join our Discord or tweet us @WeAreLabEx
This content originally appeared on DEV Community and was authored by Labby
Labby | Sciencx (2024-08-04T03:23:39+00:00) Exploring Phantom Type Parameters in Rust. Retrieved from https://www.scien.cx/2024/08/04/exploring-phantom-type-parameters-in-rust/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.