
Understand it
The report format in this exercise is intentionally smaller than CSV: one nonblank record per line, exactly two comma-separated fields, a nonempty trimmed name, and an unsigned count. Quoting and escaped commas are unsupported and must be rejected. Calling a split-on-comma parser a complete CSV parser would be misleading; real CSV permits quoted delimiters, embedded newlines, and escaped quote characters.
Parse the entire record before changing the report total. Validate the number of fields, trim both fields, check that the name is nonempty, and parse the count as u32. Report a typed or clearly documented error instead of substituting zero. At an application boundary, attach a one-based line number while preserving enough information to explain whether the structure or the number was invalid.
The compiler lab focuses on one record so its contract stays small. A later adapter can reuse the buffered-line reader, skip blank lines, and call this parser. A successful parse returns owned data that can outlive the input buffer. If ownership costs become significant, consider a borrowed intermediate representation, but measure before introducing lifetimes into every public API.
Local compiler profile: standard library only. Tests cover whitespace, zero, extra fields, missing fields, a negative count, overflow, and unsupported quotes. These cases are examples of a broader rule: input validation is a part of the data model, not a cosmetic step at the end. Before extending this format, decide whether an established CSV crate is a better fit than adding another exception to a home-grown grammar.
Read the code
fn main() {
let fields: Vec<_> = "crab, 3"
.split(',')
.map(str::trim)
.collect();
assert_eq!(fields, ["crab", "3"]);
// Validate field count, name, and count before accepting a record.
}
A bounded text format with precise errors
Try it
Return a trimmed owned name and u32 count from exactly two comma-separated fields. Reject quotes, empty names, extra/missing fields, invalid counts and overflow with Err(invalid). This is not a general CSV parser. 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
Why is split(',') insufficient for general CSV?
Reveal the answer
- CSV can contain delimiters inside quoted fields
- CSV never contains commas
- Rust strings cannot contain newlines
Quoted fields change the grammar. Use a CSV parser for the general format; keep this exercise's restrictions explicit.
Go deeper
Common misconception: Every comma-delimited file can be safely parsed with split.
A simplified grammar is useful only when its accepted and rejected inputs are explicit.
- Shape: Require exactly two fields and refuse quote characters.
- Domain: Require a name and a valid unsigned count.
- Own: Return an owned name so the record can outlive its input buffer.
Transfer challenge: Add line-number context in a streaming adapter without turning errors into skipped records.
Connections
- Save & Read a Text File: Fallible filesystem effects in a granted directory
- Serde: Decode, Validate, Encode: Serialization is not domain validation