Engineering Craft · 19 min · free with an account

API Design and Semver

Lesson preview · about 30%

A good API makes the wrong call impossible, not merely documented.

Core idea

Encode the invariant in the type; documentation is not enforcement.

Start with the idea

Newtypes stop two u64s being swapped, typestate stops a request being sent twice, and returning Result instead of panicking hands the decision to the caller. Semver then constrains what you may change: adding a public enum variant or a trait method is a breaking change, which is why non_exhaustive and sealed traits exist.

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.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Interview Prep