Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/lifetimes/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,27 @@
```rust,editable
{{#include exercise.rs:solution}}
```

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.

<details>

- **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.

</details>