C and Rust, side by side
default: absorbs any kind added later.#include <stdio.h>
enum ev_kind { EV_KEY, EV_CLICK, EV_QUIT };
struct event {
enum ev_kind kind; /* the tag */
union { char key; struct { int x, y; } click; } as;
};
static void describe(const struct event *e) {
switch (e->kind) {
case EV_KEY: printf("key %c\n", e->as.key); break;
case EV_CLICK: printf("click at %d,%d\n", e->as.click.x, e->as.click.y); break;
default: printf("ignored\n"); /* EV_QUIT and any future kind */
}
}
int main(void) {
struct event events[] = {
{EV_KEY, .as.key = 'q'},
{EV_CLICK, .as.click = {3, 4}},
{EV_QUIT, .as.key = 0},
};
for (int i = 0; i < 3; i++) describe(&events[i]);
/* Nothing ties tag to member: {EV_CLICK, .as.key = 'q'} also compiles. */
return 0;
}
Click arm is backed by an unguarded one, and the match lists every variant.enum Event {
Key(char),
Click { x: i32, y: i32 },
Quit,
}
fn describe(event: &Event) -> String {
match event { // must cover every variant
Event::Key(c) => format!("key {c}"),
Event::Click { x, y } if *x < 0 || *y < 0 => "click off-screen".to_string(),
Event::Click { x, y } => format!("click at {x},{y}"),
Event::Quit => "quit".to_string(),
}
}
fn main() {
let events = [
Event::Key('q'),
Event::Click { x: 3, y: 4 },
Event::Click { x: -1, y: 0 },
Event::Quit,
];
for event in &events {
println!("{}", describe(event));
}
}
| C | Rust |
|---|---|
| Tag and union member can disagree; nothing checks the pairing. | A variant and its data are one value; the data is reached only by matching. |
default: silently handles kinds added later. | A missing variant is error E0004; _ is an explicit opt-in. |
Cases fall through unless you write break. | Arms never fall through, and each match arm is an expression. |
Understand it
The C tagged union is a discipline: an enum tag next to a union, and every reader checks the tag before touching the matching member. Nothing enforces it. {EV_CLICK, .as.key = 'q'} compiles, reading the wrong member is not diagnosed, and a default: arm silently absorbs kinds added later, with no -Wswitch warning because a default exists.
A Rust enum makes the pairing part of the type. Key(char), Click { x, y } and Quit are variants, each with its own data, and the only way to reach that data is to match the variant; you cannot build a Click that holds a char. The compiler chooses the tag and the layout, and uses invalid bit patterns (niches) where it can, which is why Option<&T> is the size of a pointer.
match is a switch that destructures and is checked for completeness. Leave a variant out and the program does not compile (E0004, non-exhaustive patterns), so adding a variant produces a list of every place that must handle it. A _ arm opts back into C's default: behaviour; use it only when new variants really should fall through. Arms never fall through to each other, so there is no break to forget.
Arms can bind fields (Event::Click { x, y }), match literals and ranges, and add a guard such as if *x < 0. A guard narrows its arm, but exhaustiveness checking ignores guard conditions, so a guarded arm still needs an unguarded arm, or a wildcard, for the same variant. match is also an expression whose arms produce one type, which replaces a switch that assigns a variable in every case.
The C habit, written in Rust
This is how the C version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.
enum Event {
Key(char),
Click { x: i32, y: i32 },
Quit,
}
fn describe(event: &Event) -> String {
match event {
Event::Key(c) => format!("key {c}"),
Event::Click { x, y } if *x >= 0 && *y >= 0 => format!("click at {x},{y}"),
Event::Quit => "quit".to_string(),
}
}
fn main() {
println!("{}", describe(&Event::Quit));
}
Try it
Port the commented C area, including the forgotten triangle: return the area rounded down for every variant without overflowing, and make is_degenerate return true when any dimension is zero.
What success looks like: Square { side: u32::MAX } gives the exact u64 area, a 3 × 3 triangle gives 4, and zero-sized shapes are degenerate.
Check your understanding
In the practice code every variant appears in the match. Why does rustc still reject it?
Reveal the answer
- The
Clickarm has a guard, and guards do not count toward exhaustiveness, so a click with a negative coordinate is unhandled matchneeds adefaultarm, like a Cswitch- Struct-like variants cannot have guards
Quitcarries no data, so it must be listed first
Add an unguarded Event::Click { .. } arm after the guarded one. The compiler checks patterns, not the conditions in guards.
Go deeper
Common misconception: “A Rust enum is a C enum with extra syntax.”
A C enum is a set of integer constants. A Rust enum is a sum type: each variant can carry its own fields, and the compiler stores and checks the tag. Fieldless variants can still be given C-style integer values, with #[repr(...)] when the representation matters.
- Map the tags: Turn each C tag value into a variant, and move the union member it selects into that variant.
- Delete the default: Replace
default:with explicit variants, compile, and handle every place thatE0004reports. - Back each guard: For every guarded arm, make sure an unguarded arm for the same variant follows it.
Transfer challenge: Add a Circle { r: u32 } variant to Shape and let the compiler list every match to update; then decide how area should round it.
Connections
- Structs and Methods: The receiver states the pointer contract
- NULL and errno to Option and Result: Absence and failure in the return type

Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Tagged Unions to Enums (courseId "rust-for-c-developers", lessonId "c-tagged-unions-to-enums") Link: https://crabrix.com/learn/rust-for-c-developers/c-tagged-unions-to-enums/ Goal: match must be exhaustive: every variant has to be handled, and an arm with a guard does not count as covering its pattern. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-tagged-unions-to-enums) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.