{"id":18016881,"url":"https://github.com/fitzgen/peeking_take_while","last_synced_at":"2025-03-26T18:32:29.839Z","repository":{"id":47306879,"uuid":"91517025","full_name":"fitzgen/peeking_take_while","owner":"fitzgen","description":null,"archived":false,"fork":false,"pushed_at":"2021-09-03T17:57:57.000Z","size":11,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T11:59:43.057Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fitzgen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-17T00:37:49.000Z","updated_at":"2023-11-10T03:30:40.000Z","dependencies_parsed_at":"2022-08-26T07:12:09.545Z","dependency_job_id":null,"html_url":"https://github.com/fitzgen/peeking_take_while","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fpeeking_take_while","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fpeeking_take_while/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fpeeking_take_while/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fpeeking_take_while/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fitzgen","download_url":"https://codeload.github.com/fitzgen/peeking_take_while/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245713131,"owners_count":20660348,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-30T04:19:36.493Z","updated_at":"2025-03-26T18:32:29.553Z","avatar_url":"https://github.com/fitzgen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `peeking_take_while`\n\n[![Build Status](https://travis-ci.org/fitzgen/peeking_take_while.png?branch=master)](https://travis-ci.org/fitzgen/peeking_take_while)\n\nProvides the `peeking_take_while` iterator adaptor method.\n\nThe `peeking_take_while` method is very similar to `take_while`, but behaves\ndifferently when used with a borrowed iterator (perhaps returned by\n`Iterator::by_ref`).\n\n`peeking_take_while` peeks at the next item in the iterator and runs the\npredicate on that peeked item. This avoids consuming the first item yielded by\nthe underlying iterator for which the predicate returns `false`. On the other\nhand, `take_while` will consume that first item for which the predicate returns\n`false`, and it will be lost.\n\n```rust\n// Bring the `peeking_take_while` method for peekable iterators into\n// scope.\nuse peeking_take_while::PeekableExt;\n\n// Let's say we have two collections we want to iterate through: `xs` and\n// `ys`. We want to perform one operation on all the leading contiguous\n// elements that match some predicate, and a different thing with the rest of\n// the elements. With the `xs`, we will use the normal `take_while`. With the\n// `ys`, we will use `peeking_take_while`.\n\nlet xs: Vec\u003cu8\u003e = (0..100).collect();\nlet ys = xs.clone();\n\nlet mut iter_xs = xs.into_iter();\nlet mut iter_ys = ys.into_iter().peekable();\n\n{\n    // Let's do one thing with all the items that are less than 10.\n\n    let xs_less_than_ten = iter_xs.by_ref().take_while(|x| *x \u003c 10);\n    for x in xs_less_than_ten {\n        do_things_with(x);\n    }\n\n    let ys_less_than_ten = iter_ys.by_ref().peeking_take_while(|y| *y \u003c 10);\n    for y in ys_less_than_ten {\n        do_things_with(y);\n    }\n}\n\n// And now we will do some other thing with the items that are greater than\n// or equal to 10.\n\n// ...except, when using plain old `take_while` we lost 10!\nassert_eq!(iter_xs.next(), Some(11));\n\n// However, when using `peeking_take_while` we did not! Great!\nassert_eq!(iter_ys.next(), Some(10));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitzgen%2Fpeeking_take_while","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffitzgen%2Fpeeking_take_while","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitzgen%2Fpeeking_take_while/lists"}