Design a Rust command-line parser that accepts valid positive quantities and returns informative errors for invalid input.
A fictional packing tool accepts quantities as command-line strings. Values must be integers from 1 to 99. For inputs 3, 7 and 10 the total is 20; a malformed or out-of-range input rejects the whole calculation. No files are modified.
| Input set | Expected behavior |
|---|---|
| 3 7 10 | total 20 |
| 3 x 10 | error; no total |
| 0 2 | error; no total |
| 99 1 | total 100 |
Use the course navigation to study the foundations needed for a small parser. Distinguish owned strings from borrowed string slices. You do not need unsafe code or concurrency for this case. Keep the exercise scope small enough to understand every conversion and error path.
Write a function signature that accepts a slice of string inputs and returns a Result.
Parser contract
Record your result and evidence here or in a separate file.
The result type makes failure explicit.
Avoid adding lifetimes or generics without a demonstrated need.
Parsing external input can fail normally. Propagate or map the parse error into a message that names the invalid argument without crashing. Then check the business range separately. A successful parse of zero is still invalid under the case rule. The official Rust book supplies a focused supplementary explanation.
Implement parse-and-range validation for one quantity.
Quantity parser
Record your result and evidence here or in a separate file.
Malformed text and out-of-range integers return errors.
Keep parse failure distinct from range failure.
The case uses all-or-error behavior: any invalid argument prevents a total from being reported. Choose an integer type wide enough for the intended sum and document limits. A command-line adapter can print the successful total or an error and nonzero exit status without mixing the core logic with output formatting.
Implement total_quantities and a small command-line adapter.
Command-line source
Record your result and evidence here or in a separate file.
3, 7, 10 produce 20; an x input produces no success total.
Return early on an error instead of replacing it with zero.
Test valid boundaries, malformed input, an empty argument list and a rejected range. Expected results should be written before execution. This preparation environment has no Rust compiler or Cargo, so the source and checks are provided for a real local run; no compiled binary is claimed.
Run cargo test locally when available and retain the actual output.
Rust tests and execution record
Record your result and evidence here or in a separate file.
Expected output is clearly separated from actual run evidence.
Do not label source inspection as a compiler pass.
A command-line tool needs usage syntax, exit behavior and examples. Explain whether an empty input is allowed; this case rejects it because there is no quantity to summarize. Keep error messages actionable and do not expose unrelated system data. Document that the program reads arguments only and writes no files.
Write a README with valid and invalid command examples.
Tool handoff
Record your result and evidence here or in a separate file.
The usage examples agree with the implemented argument rules.
Reconcile the README and test expectations.
Deliver the source, tests, usage guide and actual local run log when a Rust toolchain is available. Add an input containing whitespace and state whether trimming is accepted.
Invalid input cannot produce a success total, and no unperformed Rust execution is claimed.
| Criterion | Points | Evidence |
|---|---|---|
| Evidence and correctness | 30 | Calculations, source use and results match the supplied case; limitations are honest. |
| Required artifacts | 30 | All five stage outputs and the capstone handoff are present and inspectable. |
| Reasoning and decisions | 20 | Choices, assumptions, alternatives and the case-specific critical condition are explained. |
| Reproducibility | 10 | A reviewer can follow the stated steps and distinguish expected from observed results. |
| Clear communication | 10 | Labels, units, structure and evidence references make the work easy to review. |
Score at least 80/100, supply every required artifact, and satisfy the path-specific critical condition. Rework missing evidence before marking complete.
The valid set 3, 7, 10 totals 20. The set 3, x, 10 returns an error rather than 13. Values 1 and 99 are valid individually; 0 and 100 are not. Rust compilation and test execution remain local learner checks in this package.
This is a small local parser, not a compiled distribution or a production command-line package.