
Understand it
Public API evolution includes downstream implementations and exhaustive matches, not just function names. Adding a required trait method or an exhaustive enum variant can break users. non_exhaustive and sealing are design choices with their own compatibility and flexibility costs.
A good API makes common correct use easy and misuse visible. A UserId newtype prevents an OrderId from being passed accidentally, even if both contain u64. A function returning Result lets callers decide how to handle expected failure instead of panicking. Ownership in signatures tells callers whether data is borrowed, mutated, or consumed. These design choices are especially important at public crate boundaries because users rely on them longer than internal helpers do.
Typestate can represent a protocol: an unconfigured request exposes configure, while only a configured request exposes send. That can prevent invalid call order, but too many type states can make errors and documentation unwieldy. Start with a clear constructor and validated data type; add typestate when the ordering rule is stable and misuse is likely. Examples should show a normal path and at least one invalid path.
For semver, adding a variant to a public exhaustive enum can break a downstream match. Adding a required method to a public trait can break downstream implementations unless a suitable default is provided. Marking an enum non_exhaustive or sealing a trait reserves evolution room, with a cost to downstream flexibility. In an interview, describe what your callers can observe, how you would migrate them, and what compile-time failures the change would cause. An API is a long-lived contract, not just a set of function names.
Show the migration path, not only the new signature. A caller-facing example reveals whether the change is truly easy to adopt.
Interview follow-up: Contrast adding a required trait method with adding a defaulted one. Which coherence or name-resolution changes can still affect downstream users?
Read the code
struct Source(u64);
struct Destination(u64);
struct Amount(u64);
fn transfer(
from: Source,
to: Destination,
amount: Amount,
) {}Swapping any two arguments no longer compiles. Wrapping the wrong id in a type still does.
Practice starter
pub enum Status { Ready, Failed }
// Another crate matches Status exhaustively.Try it
Make one plausible misuse of an API impossible to express.
What success looks like: The mistake becomes a compile error rather than a comment.
Check your understanding
Why can adding a variant to an already public exhaustive enum break downstream crates?
Reveal the answer
- Adding a variant is unconditionally non-breaking because old variants remain.
- Existing exhaustive matches may need another branch; evolution policy must be planned in advance.
- Applying non_exhaustive later is always a compatible fix for existing callers.
#[non_exhaustive] can reserve extension room when applied as part of the original contract. Adding it later can itself break exhaustive downstream code.
Go deeper
Common misconception: Applying non_exhaustive later is always a compatible fix for existing callers.
Adding #[non_exhaustive] later is itself a breaking change: downstream exhaustive matches stop compiling until they add a wildcard arm.
- Identify the contract: A good API makes the wrong call impossible, not merely documented.
- Trace the concrete example: Swapping any two arguments no longer compiles. Wrapping the wrong id in a type still does.
- Test the changed constraint: Contrast adding a required trait method with adding a defaulted one. Which coherence or name-resolution changes can still affect downstream users?
Transfer challenge: Contrast adding a required trait method with adding a defaulted one. Which coherence or name-resolution changes can still affect downstream users?
Connections
- Choosing a Test: Unit, property, fuzz, integration
- Profiling First: Measure, change one thing, measure
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: API Design and Semver (courseId "interview", lessonId "q-api-design") Link: https://crabrix.com/learn/interview/q-api-design/ Goal: Encode the invariant in the type; documentation is not enforcement. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-api-design) 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.