Skip to content
Theory · Structs · lesson 8 of 8

Tuple and unit structs

So far every field on our form has had a name: name, city, age. But some forms need no labels on their boxes. A color form has three boxes, red, green and blue, and everyone knows their order anyway. And some forms have no boxes at all, like a "Checked" stamp: only the kind of form matters.

For such cases Rust has two more forms of struct. #[derive(Debug)] works with them too:

Rust
#[derive(Debug)]
struct Color(u8, u8, u8);

#[derive(Debug)]
struct Marker;

fn main() {
    let orange = Color(255, 165, 0);
    println!("{} {} {}", orange.0, orange.1, orange.2); // 255 165 0
    println!("{:?} {:?}", orange, Marker); // Color(255, 165, 0) Marker
}

Let's go through the example line by line:

  1. struct Color(u8, u8, u8); declares a struct with three unnamed fields.
  2. struct Marker; declares a struct with no fields at all.
  3. Color(255, 165, 0) creates a value, much like a function call.
  4. orange.0, orange.1 and orange.2 read the fields by number, and the first line prints 255 165 0.
  5. {:?} prints Color(255, 165, 0), and for Marker only the type's name.

Tuple struct

A tuple struct is a struct whose fields have no names, only types. You list them in parentheses: struct Color(u8, u8, u8);. You reach the fields by number, as in a tuple, starting from zero.

How is it better than the tuple (u8, u8, u8)? Each struct is a separate type. A function that expects a Color accepts neither three random bytes nor a struct with the same fields.

Field names are still useful: orange.2 reads worse than anna.age. So a tuple struct is the choice when there are few fields and the type's name makes their order clear.

Newtype: one field

A tuple struct with one field is called a newtype: a new type on top of an old one. For example, struct Meters(u32); holds one u32 number, but to the compiler it is no longer a number but meters. A function fn walk(m: Meters) accepts only Meters, so you cannot mix up meters with seconds, or an order number with a user number.

Interviewers like to ask this: "Why do you need a newtype?" The answer: so that the compiler tells apart values that are built the same but mean different things. In memory Meters takes as much space as a u32, and the check happens only at build time.

Unit struct

A unit struct is a struct with no fields: struct Marker;. Its value is written the same as the type's name, Marker, and {:?} prints only that name. It holds no data; you need it when the type itself is what matters.

Here the form analogy falls short. A form with no boxes is nearly useless in real life, but in Rust a type with no data is common. It can have methods of its own even though it has no fields.

Common mistake

A number is passed where a newtype is expected:

Rust
struct Meters(u32);

fn walk(m: Meters) {
    println!("{} m", m.0);
}

fn main() {
    walk(5); // ✗
}
TEXT
error[E0308]: mismatched types

The compiler expects Meters and gets an integer. That is exactly why you create a newtype: a bare number cannot pass for meters. The fix is to wrap the number in the struct: walk(Meters(5)).

Key points

  • A tuple struct Color(u8, u8, u8) has unnamed fields reached by number: orange.0.
  • Each struct is a separate type, even if its fields are the same as a tuple's or another struct's.
  • A newtype is a tuple struct with one field; the compiler does not let you mix up Meters and u32.
  • A unit struct Marker has no fields, and its value is written as the type's name.