
Understand it
A few examples help explain a function, while properties describe behavior across a family of inputs. For a canonical set of integer IDs, the output must be sorted, contain no duplicates, preserve membership, and be idempotent. None of those laws alone is enough: a function returning an empty vector is sorted and idempotent but discards every ID.
Compare against an independent model such as BTreeSet rather than copying the same sort-and-deduplicate sequence into the test. Generate bounded combinations from a small domain, including negative values and repeated IDs. Small exhaustive spaces are often excellent at finding boundary mistakes, are deterministic, and require no external dependency. They still establish evidence only for the inputs tested, not a proof for every vector length.
The compiler lab's private harness checks concrete outputs and generated membership behavior. A learner cannot make it pass merely by editing an assertion in the starter. The code under test remains editable; the verdict is produced by a separate harness after calls finish. Treat this as feedback about the exercise contract, not a hostile-code containment guarantee: arbitrary source remains inside the ordinary bounded local runtime.
Local compiler profile: standard library only. On desktop Cargo, property-testing crates such as proptest can generate larger spaces and shrink a failing input into a small counterexample. Preserve the smallest failure as a named regression test even after the generator finds it. Do not call a deterministic loop a fuzzing campaign or claim complete correctness because a finite test suite passed.
Read the code
fn canonical(mut ids: Vec<i32>) -> Vec<i32> {
ids.sort_unstable();
ids.dedup();
ids
}
fn main() {
let once = canonical(vec![3, 1, 3]);
assert_eq!(once, [1, 3]);
assert_eq!(canonical(once.clone()), once);
}
An independent oracle and bounded generated cases
Try it
Return all distinct input IDs in ascending order. Preserve negative values and integer extremes; empty input stays empty. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Which property rules out a function that always returns an empty vector?
Reveal the answer
- Idempotence alone
- Sorted output alone
- Preserving input membership
Membership preservation catches lost data. Combine it with ordering and uniqueness rather than trusting a single weak property.
Go deeper
Common misconception: A single algebraic property proves the whole implementation.
Several incorrect functions satisfy one convenient property; use a complete contract and independent model.
- State laws: Specify order, uniqueness, membership, and idempotence.
- Generate: Enumerate a bounded input space reproducibly.
- Compare: Use a different data structure as the oracle.
Transfer challenge: Write a desktop proptest version and keep one shrunk failure as a deterministic regression test.
Connections
- Adversarial Parser Tests: Reject malformed data without panic or silent truncation