From c07f82d2e77992c425b26f7b756f79c32afb1679 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:09:30 +0100 Subject: [PATCH 1/2] testing: add explanatory commentary to solution This commentary, written by Gemini, focuses on aspects of the solution that differ from the baseline languages (C/Java/Python), highlighting Rust-specific idioms and concepts. --- src/testing/solution.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/testing/solution.md b/src/testing/solution.md index b4a4c92cd998..ea6074f13770 100644 --- a/src/testing/solution.md +++ b/src/testing/solution.md @@ -3,3 +3,26 @@ ```rust,editable {{#include exercise.rs:solution}} ``` + +The solution addresses several bugs in the original implementation: + +- **Whitespace:** We specifically check for and ignore whitespace using + `c.is_whitespace()`. +- **Invalid Characters:** We return `false` immediately if we encounter any + character that is not a digit and not whitespace. The original code simply + ignored them (via `else { continue }`), effectively treating "123a4" as + "1234". +- **Length Check:** We track the number of digits seen (`digits`) and return + `false` if fewer than 2 digits are found. +- **Unit Tests:** We add tests for edge cases: empty strings, strings with only + whitespace, single-digit strings, and strings with invalid characters. This + ensures the validation logic is robust. + +
+ +- Note that `c.to_digit(10)` returns `Some(digit)` if `c` is a base-10 digit, + and `None` otherwise. +- The condition `digits >= 2 && sum % 10 == 0` ensures both requirements are + met. + +
From 1d5ba26d8e587939fafe3e5f23d6b58012e82c27 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 14 Feb 2026 10:39:51 +0100 Subject: [PATCH 2/2] Refine solution commentary for experienced programmers - Remove redundant or overly simplistic explanations. - Focus on Rust-specific idioms and design choices. - Clean up formatting and technical depth. --- src/testing/solution.md | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/testing/solution.md b/src/testing/solution.md index ea6074f13770..3c346490ba2f 100644 --- a/src/testing/solution.md +++ b/src/testing/solution.md @@ -6,23 +6,24 @@ The solution addresses several bugs in the original implementation: -- **Whitespace:** We specifically check for and ignore whitespace using - `c.is_whitespace()`. -- **Invalid Characters:** We return `false` immediately if we encounter any - character that is not a digit and not whitespace. The original code simply - ignored them (via `else { continue }`), effectively treating "123a4" as - "1234". -- **Length Check:** We track the number of digits seen (`digits`) and return - `false` if fewer than 2 digits are found. -- **Unit Tests:** We add tests for edge cases: empty strings, strings with only - whitespace, single-digit strings, and strings with invalid characters. This - ensures the validation logic is robust. +- **Input Validation:** Whitespace is now handled explicitly, and invalid + characters trigger an immediate failure rather than being silently ignored. +- **Minimum Length:** The code now correctly enforces the requirement for at + least two digits. +- **Test Coverage:** Added tests for edge cases like empty strings, single + digits, and invalid characters.
-- Note that `c.to_digit(10)` returns `Some(digit)` if `c` is a base-10 digit, - and `None` otherwise. -- The condition `digits >= 2 && sum % 10 == 0` ensures both requirements are - met. +- **Unicode Safety:** Rust strings are UTF-8, so they cannot be indexed by + integer. Using `.chars().rev()` is the idiomatic way to process a string + backwards while correctly handling multi-byte characters. +- **Handling `Option`:** Using `if let Some(digit) = c.to_digit(10)` is the + standard way to handle checked conversions, providing a safe way to deal with + potential conversion failures without relying on sentinel values. +- **Iterators vs. Loops:** This algorithm could be expressed as a functional + iterator chain using `filter_map`, `enumerate`, and `fold`. However, an + imperative loop is often more readable when maintaining multiple pieces of + interdependent state like `sum`, `double`, and `digits`.