{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreigy72zg6eccs24z57rrx6ovbbixfru4rtq6ysolybwihh4ol4cov4",
    "uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mjdudfxgxmq2"
  },
  "path": "/t/pre-rfc-peeking-from-peekable-with-less-unwrapping/24169#post_1",
  "publishedAt": "2026-04-13T01:08:37.000Z",
  "site": "https://internals.rust-lang.org",
  "tags": [
    "Compiler Explorer"
  ],
  "textContent": "We have `next_if` and some other methods for conditional iteration over `Peekable`. The problem is that these methods are not really composable. Usually it is easier to use `peek`, do something with the peeked value and then do `.next().unwrap()`. This `unwrap` feels unnecessary and never fails if the value is already `peek`ed and known to be there. Hence the following idea.\n\nI propose to add a method for `Peekable<I>` with the following signature (the names here are temporary, of course):\n\n\n    fn peek_2(&'a mut self) -> Option<Peeked<'a, I>> { ... }\n\n\n`Peeked<'a, I>` is a type with\n\n\n    fn get(&self) -> &'a I::Item { ... }\n    fn consume(self) -> I::Item { ... }\n\n\nCalling the first method is equivalent to `peek()`ing. The second one equivalent to `next()`, but there is no unnecessary `Option` now.\n\nI think the described interface cannot be implemented without using `Peekable`'s internals or `unsafe`. Here is an implementation sketch with `unsafe`: Compiler Explorer. There is also an implementation of \"merge iterator\" using the proposed interface.",
  "title": "[Pre-RFC] Peeking from Peekable with less unwrapping"
}