From 45d8465f9374b08c6e1508b19d55c44441b2d384 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:02:48 +0100 Subject: [PATCH 1/2] lifetimes: 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/lifetimes/solution.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/lifetimes/solution.md b/src/lifetimes/solution.md index b4a4c92cd998..a08c0b89f988 100644 --- a/src/lifetimes/solution.md +++ b/src/lifetimes/solution.md @@ -3,3 +3,26 @@ ```rust,editable {{#include exercise.rs:solution}} ``` + +- **Zero-Copy Parsing:** This solution demonstrates zero-copy parsing. The + `Person` and `PhoneNumber` structs borrow string slices (`&'a str`) directly + from the input byte array (`&'a [u8]`). No new strings are allocated for the + field values. +- **Lifetimes:** The lifetime `'a` ensures that the returned `Person` cannot + outlive the input data buffer. If the buffer is dropped, the `Person` becomes + invalid, which the compiler enforces. +- **Slicing:** We use `split_at` to divide the input buffer into the data for + the current field and the remainder. This is efficient as it only manipulates + pointers and lengths, not the data itself. +- **Recursion:** The parsing is recursive. `Person` contains `PhoneNumber`s, + which are themselves parsed by calling `parse_message` on the bytes contained + within the `Len` wire type. + +
+ +- Note that `parse_message` requires `T` to implement `Default` so it can start + with an empty message and fill it in. +- The `WireType` enum and `parse_varint` function handle the low-level encoding + details, allowing the main logic to focus on structure. + +
From 327dc9d25b9eddd96d04abca63a429fb2a1960f0 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 14 Feb 2026 10:39:45 +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/lifetimes/solution.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/lifetimes/solution.md b/src/lifetimes/solution.md index a08c0b89f988..0c583f246d70 100644 --- a/src/lifetimes/solution.md +++ b/src/lifetimes/solution.md @@ -4,25 +4,26 @@ {{#include exercise.rs:solution}} ``` -- **Zero-Copy Parsing:** This solution demonstrates zero-copy parsing. The - `Person` and `PhoneNumber` structs borrow string slices (`&'a str`) directly - from the input byte array (`&'a [u8]`). No new strings are allocated for the - field values. -- **Lifetimes:** The lifetime `'a` ensures that the returned `Person` cannot - outlive the input data buffer. If the buffer is dropped, the `Person` becomes - invalid, which the compiler enforces. -- **Slicing:** We use `split_at` to divide the input buffer into the data for - the current field and the remainder. This is efficient as it only manipulates - pointers and lengths, not the data itself. -- **Recursion:** The parsing is recursive. `Person` contains `PhoneNumber`s, - which are themselves parsed by calling `parse_message` on the bytes contained - within the `Len` wire type. +The solution demonstrates zero-copy parsing, a high-performance pattern made +safe by Rust's lifetime system: + +- **Zero-Copy Parsing:** The `Person` and `PhoneNumber` structs do not own their + strings; instead, they borrow them directly from the input byte array. No + allocations are performed during parsing. +- **Enforced Safety:** The lifetime parameter `'a` ties the output structures to + the input buffer. The compiler ensures that a `Person` cannot outlive the + bytes it was parsed from, preventing use-after-free bugs. +- **Efficient Slicing:** Using `split_at` allows for efficient traversal of the + input buffer by manipulating pointers and lengths without copying data.
-- Note that `parse_message` requires `T` to implement `Default` so it can start - with an empty message and fill it in. -- The `WireType` enum and `parse_varint` function handle the low-level encoding - details, allowing the main logic to focus on structure. +- **Lifetime Propagation:** Note how the lifetime `'a` is propagated through the + recursive `parse_message` and `add_field` calls. This creates a chain of + borrows that the borrow checker tracks throughout the entire execution. +- **Trade-offs:** While zero-copy parsing is extremely fast, it requires the + original input buffer to remain in memory as long as the parsed data is in + use. In some applications, copying into owned `String`s might be preferable to + free up the input buffer sooner.