From b6a357c4319cb3acd9ad5a82f647eae05aab7664 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:27:18 -0800 Subject: [PATCH 01/26] Fix C++ this. to this-> (compilation error) In C++ 'this' is a pointer, not a reference. Using 'this.member' will not compile. Changed to 'this->member' in inheritance_and_reuse.md and object_identity.md. Co-Authored-By: Claude Opus 4.6 --- src/idioms/data_modeling/inheritance_and_reuse.md | 4 ++-- src/idioms/object_identity.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/idioms/data_modeling/inheritance_and_reuse.md b/src/idioms/data_modeling/inheritance_and_reuse.md index debb1a6..e1a5870 100644 --- a/src/idioms/data_modeling/inheritance_and_reuse.md +++ b/src/idioms/data_modeling/inheritance_and_reuse.md @@ -33,12 +33,12 @@ class Printer : public Device { bool powered = false; public: void powerOn() override { - this.powered = true; + this->powered = true; std::cout << "Printer is powered on." << std::endl; } void powerOff() override { - this.powered = false; + this->powered = false; std::cout << "Printer is powered off." << std::endl; } }; diff --git a/src/idioms/object_identity.md b/src/idioms/object_identity.md index c97a914..f70b4c3 100644 --- a/src/idioms/object_identity.md +++ b/src/idioms/object_identity.md @@ -40,7 +40,7 @@ struct Person Person& operator=(const Person& other) { // compare object identity first if (this != &other) { - this.name = other.name; + this->name = other.name; // copy the other expensive-to-copy fields } From 25f9d65f768b1580e53dadab0dd1f7b57386354a Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:27:32 -0800 Subject: [PATCH 02/26] Fix uninitialized loop variable in C++ example (UB) The for loops in placement_new.md declared 'std::size_t i;' without initialization, producing undefined behavior. Added '= 0' to both loop variable declarations. Co-Authored-By: Claude Opus 4.6 --- src/idioms/placement_new.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/idioms/placement_new.md b/src/idioms/placement_new.md index e222326..4768d72 100644 --- a/src/idioms/placement_new.md +++ b/src/idioms/placement_new.md @@ -169,12 +169,12 @@ int main() { constexpr unsigned int SIZE = 8000000; std::unique_ptr b = std::make_unique< std::array>(); - for (std::size_t i; i < SIZE; ++i) { + for (std::size_t i = 0; i < SIZE; ++i) { (*b)[i] = 42; } // use b so that it isn't optimized away - for (std::size_t i; i < SIZE; ++i) { + for (std::size_t i = 0; i < SIZE; ++i) { std::cout << (*b)[i] << std::endl; } } From 698ea57c5bee7204ad1f2dc7f36e5c92bf2edf48 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:27:48 -0800 Subject: [PATCH 03/26] Fix twice() computing square instead of double The function named 'twice' was computing n * n (squaring) instead of n * 2 (doubling). Fixed in both the C++ and Rust examples in the function-to-pointer section of promotions_and_conversions.md. Co-Authored-By: Claude Opus 4.6 --- src/idioms/promotions_and_conversions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/idioms/promotions_and_conversions.md b/src/idioms/promotions_and_conversions.md index 4337a04..b9c42aa 100644 --- a/src/idioms/promotions_and_conversions.md +++ b/src/idioms/promotions_and_conversions.md @@ -99,7 +99,7 @@ do not have function type, but can also be converted to function pointers. ```cpp int twice(int n) { - return n * n; + return n * 2; } struct MyPair { @@ -139,7 +139,7 @@ int main() { ```rust fn twice(x: i32) -> i32 { - x * x + x * 2 } struct MyPair(i32, i32); From bee11aee0eab0c35c4e6e18c7fca821124d104e1 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:27:59 -0800 Subject: [PATCH 04/26] Fix normalization math error in setters_and_getters example The unit vector check was computing x^2 + xy instead of x^2 + y^2. Changed 'v.y * v.x' to 'v.y * v.y' in the Normalized::from_vec2 method. Co-Authored-By: Claude Opus 4.6 --- src/idioms/encapsulation/setters_and_getters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/idioms/encapsulation/setters_and_getters.md b/src/idioms/encapsulation/setters_and_getters.md index 9fa14ac..506033c 100644 --- a/src/idioms/encapsulation/setters_and_getters.md +++ b/src/idioms/encapsulation/setters_and_getters.md @@ -119,7 +119,7 @@ fn sqrt_approx_zero(x: f64) -> bool { impl Normalized { pub fn from_vec2(v: Vec2) -> Option { - if sqrt_approx_zero(v.x * v.x + v.y * v.x - 1.0) { + if sqrt_approx_zero(v.x * v.x + v.y * v.y - 1.0) { Some(Self(v)) } else { None From 0c370cd67bb534cbca3a51bacb6ff4ea68016179 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:28:13 -0800 Subject: [PATCH 05/26] Fix Optional> to Option> in sentinel_values Used the C++ name 'Optional' instead of the Rust type name 'Option'. Co-Authored-By: Claude Opus 4.6 --- src/idioms/null/sentinel_values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/idioms/null/sentinel_values.md b/src/idioms/null/sentinel_values.md index 9b0a18c..751ba6f 100644 --- a/src/idioms/null/sentinel_values.md +++ b/src/idioms/null/sentinel_values.md @@ -53,7 +53,7 @@ The `Box` type has the same meaning as `std::unique_ptr` in terms of being a uniquely owned pointer to some `T` on the heap, but unlike `std::unique_ptr`, it cannot be null. Rust's `Option` (which is similar to `std::optional` in C++) can represent optional pointers when used in conjunction with `Box`, as -in `Optional>`. In [those cases (and in some other +in `Option>`. In [those cases (and in some other cases)](../data_modeling/template_specialization.md#niche-optimization) the compiler optimizes the representation to be the same size as `Box` by leveraging the fact that `Box` cannot be null. From 6557dac3a78087437c7f2d44f8deaadc5291b10b Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:28:53 -0800 Subject: [PATCH 06/26] Fix ErrorB copy-paste error showing ErrorA message Three instances of the ErrorB C++ struct had a message string saying "ErrorA was produced" instead of "ErrorB was produced". Co-Authored-By: Claude Opus 4.6 --- src/idioms/exceptions/expected_errors.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/idioms/exceptions/expected_errors.md b/src/idioms/exceptions/expected_errors.md index 2f2f344..509ceb7 100644 --- a/src/idioms/exceptions/expected_errors.md +++ b/src/idioms/exceptions/expected_errors.md @@ -447,7 +447,7 @@ struct ErrorA : public std::exception { void mightThrowA() {} struct ErrorB : public std::exception { - const char *msg = "ErrorA was produced"; + const char *msg = "ErrorB was produced"; const char *what() const noexcept override { return msg; } @@ -565,7 +565,7 @@ struct ErrorA : public std::exception { void mightThrowA() {} struct ErrorB : public std::exception { - const char *msg = "ErrorA was produced"; + const char *msg = "ErrorB was produced"; const char *what() const noexcept override { return msg; } @@ -645,7 +645,7 @@ struct ErrorA : public std::exception { void mightThrowA() {} struct ErrorB : public std::exception { - const char *msg = "ErrorA was produced"; + const char *msg = "ErrorB was produced"; const char *what() const noexcept override { return msg; } From 40ae09d80af6e98c0870ed8eaa7a66caa88a3944 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:29:08 -0800 Subject: [PATCH 07/26] Fix Rust snake_case violations in expected_errors examples Renamed might_throw_A and might_throw_B to might_throw_a and might_throw_b to follow Rust's snake_case naming convention. Co-Authored-By: Claude Opus 4.6 --- src/idioms/exceptions/expected_errors.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/idioms/exceptions/expected_errors.md b/src/idioms/exceptions/expected_errors.md index 509ceb7..17a660b 100644 --- a/src/idioms/exceptions/expected_errors.md +++ b/src/idioms/exceptions/expected_errors.md @@ -480,7 +480,7 @@ impl Display for ErrorA { impl Error for ErrorA {} -fn might_throw_A() -> Result<(), ErrorA> { +fn might_throw_a() -> Result<(), ErrorA> { Ok(()) } @@ -498,7 +498,7 @@ impl Display for ErrorB { impl Error for ErrorB {} -fn might_throw_B() -> Result<(), ErrorB> { +fn might_throw_b() -> Result<(), ErrorB> { Ok(()) } @@ -537,8 +537,8 @@ impl From for ErrorAOrB { fn process() -> Result<(), ErrorAOrB> { // the ? operator uses the From instance - might_throw_A()?; - might_throw_B()?; + might_throw_a()?; + might_throw_b()?; Ok(()) } ``` @@ -586,7 +586,7 @@ use thiserror::Error; #[error("ErrorA was produced")] struct ErrorA; -fn might_throw_A() -> Result<(), ErrorA> { +fn might_throw_a() -> Result<(), ErrorA> { Ok(()) } @@ -594,7 +594,7 @@ fn might_throw_A() -> Result<(), ErrorA> { #[error("ErrorB was produced")] struct ErrorB; -fn might_throw_B() -> Result<(), ErrorB> { +fn might_throw_b() -> Result<(), ErrorB> { Ok(()) } @@ -607,8 +607,8 @@ enum ErrorAOrB { } fn process() -> Result<(), ErrorAOrB> { - might_throw_A()?; - might_throw_B()?; + might_throw_a()?; + might_throw_b()?; Ok(()) } ``` @@ -676,7 +676,7 @@ use thiserror::Error; #[error("ErrorA was produced")] struct ErrorA; -fn might_throw_A() -> Result<(), ErrorA> { +fn might_throw_a() -> Result<(), ErrorA> { Ok(()) } @@ -684,13 +684,13 @@ fn might_throw_A() -> Result<(), ErrorA> { #[error("ErrorB was produced")] struct ErrorB; -fn might_throw_B() -> Result<(), ErrorB> { +fn might_throw_b() -> Result<(), ErrorB> { Ok(()) } fn process() -> anyhow::Result<()> { - might_throw_A()?; - might_throw_B()?; + might_throw_a()?; + might_throw_b()?; Ok(()) } From fa7dac3739621926b0a4caea1574889efdc4dd29 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:29:24 -0800 Subject: [PATCH 08/26] Fix Rust camelCase variable names to snake_case Renamed twicePtr, ctorPtr, methodPtr, closureRes to twice_ptr, ctor_ptr, method_ptr, closure_res in the function-to-pointer Rust example in promotions_and_conversions.md. Co-Authored-By: Claude Opus 4.6 --- src/idioms/promotions_and_conversions.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/idioms/promotions_and_conversions.md b/src/idioms/promotions_and_conversions.md index b9c42aa..81f3278 100644 --- a/src/idioms/promotions_and_conversions.md +++ b/src/idioms/promotions_and_conversions.md @@ -152,22 +152,22 @@ impl MyPair { fn main() { // convert a function to a function pointer - let twicePtr: fn(i32) -> i32 = twice; - let res = twicePtr(5); + let twice_ptr: fn(i32) -> i32 = twice; + let res = twice_ptr(5); // convert a constructor to a function pointer - let ctorPtr: fn(i32, i32) -> MyPair = MyPair; - let pair = ctorPtr(10, 20); + let ctor_ptr: fn(i32, i32) -> MyPair = MyPair; + let pair = ctor_ptr(10, 20); // convert a static method to a function // pointer - let methodPtr: fn() -> MyPair = MyPair::new; - let pair2 = methodPtr(); + let method_ptr: fn() -> MyPair = MyPair::new; + let pair2 = method_ptr(); // convert a non-capturing closure to a // function pointer let closure: fn(i32) -> i32 = |x: i32| x * 5; - let closureRes = closure(2); + let closure_res = closure(2); } ``` From 94492d1de9ca8f803444e8e4c062c56ebd9e10d1 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:29:37 -0800 Subject: [PATCH 09/26] Fix subject-verb agreement in abstract_classes.md Changed 'both requires' to 'both require'. Co-Authored-By: Claude Opus 4.6 --- src/idioms/data_modeling/abstract_classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/idioms/data_modeling/abstract_classes.md b/src/idioms/data_modeling/abstract_classes.md index e2ddaad..963ccfc 100644 --- a/src/idioms/data_modeling/abstract_classes.md +++ b/src/idioms/data_modeling/abstract_classes.md @@ -165,7 +165,7 @@ given by a user defined `Drop` implementation or not) required for the value. ## Vtables and Rust trait object types -C++ and Rust both requires some kind of indirection to perform dynamic dispatch +C++ and Rust both require some kind of indirection to perform dynamic dispatch against an interface. In C++ this indirection takes the form of a pointer to the abstract class (instead of the derived concrete class), making use of a vtable to resolve the virtual method. From ef1a3c6dce415e57bae54bb25a29195bc827c907 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Feb 2026 11:29:55 -0800 Subject: [PATCH 10/26] Fix typos in templates.md - 'idomatic' -> 'idiomatic' - 'defintion' -> 'definition' - 'using and Rust's' -> 'using Rust's' (extra word) - 'C++ template' -> 'a C++ template' (missing article) Co-Authored-By: Claude Opus 4.6 --- src/idioms/data_modeling/templates.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/idioms/data_modeling/templates.md b/src/idioms/data_modeling/templates.md index 79f83fc..efd30fe 100644 --- a/src/idioms/data_modeling/templates.md +++ b/src/idioms/data_modeling/templates.md @@ -91,7 +91,7 @@ impl