expression

Transparent representation for a newtype

A transparent wrapper around one ordinary field, with only permitted zero-sized alignment-one companions, retains the represented field layout and ABI. The wrapper is still a distinct Rust type; its own logical invariants must be respected. Expected stdout (not executed): 25 true true Common mistake: Treating a transparent wrapper as proof that all constructors uphold its domain rules.

Formal Syntax & Structure

rust syntaxrust
#[repr(transparent)]

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::mem::{align_of, size_of};    #[repr(transparent)]    struct Millimeters(u32);    let length = Millimeters(25);    println!("{} {} {}", length.0, size_of::<Millimeters>() == size_of::<u32>(), align_of::<Millimeters>() == align_of::<u32>());}
Rust Reference