When Rust rejects a returned reference, start by finding the value that owns the bytes. Then check whether the function signature gives the caller enough information to keep that owner alive. A borrowed field can often be returned directly from its input. A field assembled in local scratch storage needs a different contract. An iterator may borrow its source only until its last use, or longer if you keep one of the references it yields.
The exercises below follow a log parser through those three situations. The PracHub string ownership exercise is a useful warm-up. Identify who owns the storage, then explain what happens when you move the owner versus copying its contents. The language rules here come from the Rust documentation. The parser and follow-up cases are original practice.
Start with a caller the parser should accept
You receive log lines such as "svc:ready" . A setting tells the parser which prefix to strip. The function should return a borrowed suffix of the line.…