
Understand it
Suppose a preview function once sliced text at a byte offset and panicked on an accented character. Replacing it with a character iterator fixes that specific bug, but keep a regression that would fail under the old implementation. A test named after the behavior, such as truncates_at_scalar_boundary, is more useful than one named after an unrelated ticket number.
The contract here is to keep at most n Unicode scalar values and add no ellipsis. Return all text when it is shorter than n, and an empty string when n is zero. This is not a display-column limit or a grapheme-cluster limit. An e followed by a combining accent consists of two scalar values; stopping after one may produce a visually surprising but contractually valid result. A user-interface preview may need a grapheme-aware library instead.
A strong regression set contains the smallest original failure, zero, an empty input, an oversized limit, and a neighboring ASCII case. Add a property that the result is a prefix at a valid UTF-8 boundary and contains at most the requested number of scalar values. Avoid assuming that String::len counts characters; it counts bytes.
Local compiler profile: standard library only. The lab compiles the changed function and checks the regression cases independently. Before changing the contract to add an ellipsis, decide whether the ellipsis counts toward the limit and whether existing callers depend on exact output. Good tests document that contract; they should not freeze every internal implementation choice or discourage a clearer iterator-based solution.
Read the code
fn prefix(text: &str, n: usize) -> String {
text.chars().take(n).collect()
}
fn main() {
assert_eq!(prefix("É🦀rust", 2), "É🦀");
assert_eq!(prefix("abc", 0), "");
}
A minimal failure that survives a refactor
Try it
Return at most n Unicode scalar values from the start, with no ellipsis. Never slice at an unchecked byte offset. 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
How many Unicode scalar values are in e followed by a combining accent?
Reveal the answer
- One in every representation
- Two, although they may render as one grapheme
- The UTF-8 byte length is always the scalar count
A scalar value and a grapheme are different units. State and test the intended unit explicitly.
Go deeper
Common misconception: chars().count() always measures visible character width.
Scalar values can combine into a grapheme and have different display widths.
- Reproduce: Keep the smallest input that broke byte slicing.
- Specify: Define zero, short input, and the Unicode unit.
- Protect: Retain the regression while allowing the implementation to change.
Transfer challenge: Design a grapheme-aware version for desktop Cargo. Explain how its output differs for combining accents and emoji sequences.
Connections
- Adversarial Parser Tests: Reject malformed data without panic or silent truncation