What PhantomContravariant does
PhantomContravariant is a struct exposed by the std::marker module of the Rust standard library. It is part of the public, stable API and you can rely on it from any edition of Rust.
As a struct, PhantomContravariant bundles related fields into a single named record with a known layout. Each field has a fixed type and offset, so the size of an instance is the sum of its field sizes plus alignment padding. Construction follows ordinary Rust ownership rules: each owned field must itself be constructed or moved before the struct is well-formed, and the compiler will refuse to leave any field uninitialised. The full canonical path is std::marker::PhantomContravariant; bring it into scope with use std::marker::PhantomContravariant; or refer to it by its full path.
When to use it
Reach for PhantomContravariant whenever you need a value with the fields and methods documented for std::marker::PhantomContravariant. Use a reference (`&PhantomContravariant`) in function parameters by default; only take ownership when the function must consume or store the value.
Annotated examples
Constructing and using PhantomContravariant
use std::marker::PhantomContravariant;
fn demo() {
// Construct a PhantomContravariant value. The exact constructor depends on the type;
// look for `new`, `default`, `from`, or `with_*` associated functions in
// the documentation. For types with no public constructor, you typically
// receive a PhantomContravariant by calling another std function that returns one.
let value: PhantomContravariant = Default::default();
// Most std structs implement Debug, so {:?} works for inspection.
println!("{:?}", value);
}
The first thing you do with any new struct is figure out how to construct one — `new`, `default`, `from`, or returned from another function. The std::marker::PhantomContravariant reference shows every available constructor.
For deeper background, see the canonical Rust patterns reference for the broader context behind this section.
Passing PhantomContravariant into a function
use std::marker::PhantomContravariant;
// Take PhantomContravariant by reference when you only need to read it — this avoids
// any clone or move and lets the caller keep using their value.
fn observe(value: &PhantomContravariant) {
println!("{:?}", value);
}
// Take PhantomContravariant by &mut when you need to mutate it in place.
fn update(value: &mut PhantomContravariant) {
*value = Default::default();
}
// Take PhantomContravariant by value only when you need to consume it.
fn consume(value: PhantomContravariant) -> PhantomContravariant {
value
}
The borrow / mutate / consume distinction is the most important Rust ergonomic to internalise. Default to `&PhantomContravariant`, escalate to `&mut PhantomContravariant` when you must mutate, and only take ownership when you must.
Common pitfalls
Avoid cloning a PhantomContravariant unnecessarily — pass it by `&PhantomContravariant` whenever read-only access is enough. If the struct holds heap allocations, every clone duplicates that memory. Hold no more than one `&mut` borrow at a time; the compiler will refuse to compile aliased mutable references.
For deeper background, see an in-depth Rust idioms cheat sheet for the broader context behind this section.
Performance & threading notes
Stack-allocated by default. Heap allocation only happens if a field is itself heap-allocated (Vec, String, Box). Cloning copies every field; for read access pass by reference. Send / Sync are automatically derived if every field is Send / Sync.