Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.

Unit structs have no fields. They are used as type markers or to implement traits without carrying data. PHP has no direct equivalent; marker classes or empty classes are the closest.

Use case: marker types with no payload

Sometimes you only care about which kind something is, not extra data, like a PHP empty marker class you type-hint so AlwaysEqual and MarkedEvent are never confused. A unit struct is that idea in Rust: AlwaysEqual and MarkedEvent are different types, take no runtime storage (they’re zero-sized), and you can still give them different behaviour later (impl blocks, traits). Contrast with (): there is only one unit type, so it can’t distinguish “kinds” the way two unit structs can.

Is it like an empty PHP interface? Kind of, the usual pattern is a marker: you care about the type name for checks or APIs, not fields or methods yet. In Rust, methods / shared behaviour come from trait + impl, not from the struct having data; the unit struct itself is just the distinct, zero-sized label.

Rust

struct AlwaysEqual;
struct MarkedEvent;

fn describe_always_equal(_: &AlwaysEqual) -> &'static str {
    "comparison marker (always equal)"
}

fn describe_marked_event(_: &MarkedEvent) -> &'static str {
    "event marker (no fields attached)"
}

fn main() {
    let subject = AlwaysEqual;
    let event = MarkedEvent;

    println!("{}", describe_always_equal(&subject));
    println!("{}", describe_marked_event(&event));
}

PHP

<?php
// Empty class as marker
class AlwaysEqual {}
class MarkedEvent {}

$subject = new AlwaysEqual();
$event = new MarkedEvent();

Comparison

  • Rust: No () needed when constructing; AlwaysEqual is the constructor.
  • PHP: Requires new and parentheses; empty classes act as markers.
  • Rust: Often used to implement traits (e.g. Default) or to distinguish types.
  • PHP: Similar uses for marker interfaces or empty base classes.

References

What's next

Next: Struct initialization and update syntax.

From PHP to Rust — Rust Syntax and Functions