Tuple structs
A tuple struct has a name and positional fields, written struct UserId(u64). It is the usual way to wrap one type so the compiler will not mix it with another.
Detailed Explanation
A named-field struct is struct Session { user_id: u64 }. A tuple struct is struct UserId(u64). You construct it with UserId(17) and you read the inner value with id.0 or by destructuring let UserId(raw) = id. There are no field names. The name of the type is the meaning.
The one-field form is a newtype. UserId and SessionId can both wrap u64 and remain incompatible. A function that takes UserId will not accept a raw u64 or a SessionId. That is the point: you push a class of mix-up from runtime to compile time. Units of measure, database ids, and validated strings are the usual uses.
Privacy still applies. struct UserId(u64) with a private field — the default — prevents id.0 outside the module, so callers cannot forge an id without going through UserId::new. struct UserId(pub u64) publishes the inner field and is the form you want when the wrapper is only for type distinction, not for validation.
Tuple structs may have several fields: struct Colour(u8, u8, u8). That is a named tuple. Prefer named fields once you cannot remember what .1 is. The multi-field tuple struct is the right shape when the positions are conventional, as in a point (x, y) you will destructure everywhere.
You can #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] on a newtype and treat it like the inner type for those traits. You do not get the inner type's methods automatically. UserId(17).saturating_add(1) does not compile. Implement Deref<Target = u64> only if you are sure you want method forwarding; many teams prefer explicit as_u64() so the wrapper does not quietly accept every u64 method, including the ones that break the invariant.
When you reach for a tuple struct: a wrapper around one existing type, or a tiny positional product with an obvious order. When you need names, a regular struct. When you need no data at all, a unit struct.
Code Examples
struct UserId(u64);
struct SessionId(u64);
fn lookup(user: UserId) -> u64 {
user.0
}
fn main() {
let user = UserId(17);
// lookup(SessionId(17)) would not compile: different type
println!("{}", lookup(user)); // prints: 17
let UserId(raw) = UserId(99);
println!("{raw}"); // prints: 99
}Common Gotchas
`.0` is the field name
There is no `.value` unless you write a method. Destructuring `let UserId(raw) = id` reads more clearly than `id.0` in many functions.
`Deref` to the inner type punches a hole
With `Deref<Target = u64>`, a `UserId` starts accepting `u64` methods and some coercions. Prefer an explicit getter if the wrapper exists to restrict operations.
A public inner field can be forged
`UserId(pub u64)` lets anyone write `UserId(0)`. Keep the field private when zero or empty is invalid.
Newtypes do not change layout
`UserId` is the same size as `u64`. The wrapper is a type-system tool, not an allocation.