about summary refs log tree commit diff
path: root/library/core/src/iter/mod.rs
AgeCommit message (Collapse)AuthorLines
2025-09-26Update CURRENT_RUSTC_VERSION post-bumpMark Rousskov-1/+1
2025-08-05Stabilize `core::iter::chain`Ross MacArthur-1/+1
2025-07-25Improve and regularize comment placement in doc codeJosh Triplett-2/+4
Because doc code does not get automatically formatted, some doc code has creative placements of comments that automatic formatting can't handle. Reformat those comments to make the resulting code support standard Rust formatting without breaking; this is generally an improvement to readability as well. Some comments are not indented to the prevailing indent, and are instead aligned under some bit of code. Indent them to the prevailing indent, and put spaces *inside* the comments to align them with code. Some comments span several lines of code (which aren't the line the comment is about) and expect alignment. Reformat them into one comment not broken up by unrelated intervening code. Some comments are placed on the same line as an opening brace, placing them effectively inside the subsequent block, such that formatting would typically format them like a line of that block. Move those comments to attach them to what they apply to. Some comments are placed on the same line as a one-line braced block, effectively attaching them to the closing brace, even though they're about the code inside the block. Reformat to make sure the comment will stay on the same line as the code it's commenting.
2025-06-26Tracking issue number for `iter_macro`Pavel Grigorenko-1/+1
2025-06-03Add `iter` macroOli Scherer-0/+2
This adds an `iter!` macro that can be used to create movable generators. This also adds a yield_expr feature so the `yield` keyword can be used within iter! macro bodies. This was needed because several unstable features each need `yield` expressions, so this allows us to stabilize them separately from any individual feature. Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de> Co-authored-by: Jieyou Xu <jieyouxu@outlook.com> Co-authored-by: Travis Cross <tc@traviscross.com>
2025-01-18re-export `core::iter::FromCoroutine`joseLuís-3/+3
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-13/+13
2024-09-03replace placeholder versionBoxy-1/+1
2024-08-19Stabilize `iter::repeat_n`Scott McMurray-1/+1
2024-07-29Reformat `use` declarations.Nicholas Nethercote-41/+36
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-06-04Add function `core::iter::chain`Ross MacArthur-0/+2
The addition of `core::iter::zip` (#82917) set a precedent for adding plain functions for iterator adaptors. Adding `chain` makes it a little easier to `chain` two iterators. ``` for (x, y) in chain(xs, ys) {} // vs. for (x, y) in xs.into_iter().chain(ys) {} ```
2023-11-28Auto merge of #110353 - the8472:in-place-flatten-chunks, r=cuviperbors-0/+2
Expand in-place iteration specialization to Flatten, FlatMap and ArrayChunks This enables the following cases to collect in-place: ```rust let v = vec![[0u8; 4]; 1024] let v: Vec<_> = v.into_iter().flatten().collect(); let v: Vec<Option<NonZeroUsize>> = vec![NonZeroUsize::new(0); 1024]; let v: Vec<_> = v.into_iter().flatten().collect(); let v = vec![u8; 4096]; let v: Vec<_> = v.into_iter().array_chunks::<4>().collect(); ``` Especially the nicheful-option-flattening should be useful in real code.
2023-10-20s/generator/coroutine/Oli Scherer-3/+3
2023-09-03Expand in-place iteration specialization to Flatten, FlatMap and ArrayChunksThe 8472-0/+2
2023-08-11Add Iterator::map_windowsFrank King-0/+2
This is inherited from the old PR. This method returns an iterator over mapped windows of the starting iterator. Adding the more straight-forward `Iterator::windows` is not easily possible right now as the items are stored in the iterator type, meaning the `next` call would return references to `self`. This is not allowed by the current `Iterator` trait design. This might change once GATs have landed. The idea has been brought up by @m-ou-se here: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Iterator.3A.3A.7Bpairwise.2C.20windows.7D/near/224587771 Co-authored-by: Lukas Kalbertodt <lukas.kalbertodt@gmail.com>
2023-05-04Optimize `Iterator` implementation for `&mut impl Iterator + Sized`Benoît du Garreau-0/+6
2023-03-15Bump to latest betaMark Rousskov-1/+1
2023-02-23Add lint against `Iterator::map` receiving a callable that returns `()`Obei Sideg-0/+1
2023-02-04Allow canonicalizing the `array::map` loop in trusted casesScott McMurray-0/+1
2023-02-03Replace `ConstFnMutClosure` with const closuresDeadbeef-4/+2
2022-11-15`VecDeque::resize` should re-use the buffer in the passed-in elementScott McMurray-0/+2
Today it always copies it for *every* appended element, but one of those clones is avoidable.
2022-09-26Use a macro to not have to copy-paste `ConstFnMutClosure::new(&mut fold, ↵Scott McMurray-0/+23
NeverShortCircuit::wrap_mut_2_imp)).0` everywhere Also use that macro to replace a bunch of places that had custom closure-wrappers.
2022-08-12fill-in tracking issue for `feature(iter_array_chunks)`Maybe Waffle-1/+1
2022-08-01Add `Iterator::array_chunks()`Ross MacArthur-0/+2
2022-06-18Expose iter::ByRefSized as unstable feature and use itPaolo Barbolini-1/+3
2022-05-27libcore: Add `iter::from_generator` which is like `iter::from_fn`, but for ↵Vadim Petrochenkov-0/+6
coroutines instead of functions
2022-03-26Remove mention of HashMap<K, V> not offering iter_mutdlup-4/+3
2022-03-18Rollup merge of #94115 - scottmcm:iter-process-by-ref, r=yaahcMatthias Krüger-1/+1
Let `try_collect` take advantage of `try_fold` overrides No public API changes. With this change, `try_collect` (#94047) is no longer going through the `impl Iterator for &mut impl Iterator`, and thus will be able to use `try_fold` overrides instead of being forced through `next` for every element. Here's the test added, to see that it fails before this PR (once a new enough nightly is out): https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=462f2896f2fed2c238ee63ca1a7e7c56 This might as well go to the same person as my last `try_process` PR (#93572), so r? ``@yaahc``
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-8/+8
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-10Let `try_collect` take advantage of `try_fold` overridesScott McMurray-1/+1
Without this change it's going through `&mut impl Iterator`, which handles `?Sized` and thus currently can't forward generics. Here's the test added, to see that it fails before this PR (once a new enough nightly is out): https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=462f2896f2fed2c238ee63ca1a7e7c56
2022-02-07Change `ResultShunt` to be generic over `Try`Scott McMurray-1/+1
Just a refactor (and rename) for now, so it's not `Result`-specific. This could be used for a future `Iterator::try_collect`, or similar, but anything like that is left for a future PR.
2021-12-14Stabilize iter::zip.PFPoitras-1/+1
2021-10-07Revert "Stabilize `Iterator::intersperse()`"Jane Lusby-1/+1
2021-09-25Auto merge of #88343 - steffahn:fix_code_spacing, r=jyn514bors-1/+1
Fix spacing of links in inline code. Similar to #80733, but the focus is different. This PR eliminates all occurrences of pieced-together inline code blocks like [`Box`]`<`[`Option`]`<T>>` and replaces them with good-looking ones (using HTML-syntax), like <code>[Box]<[Option]\<T>></code>. As far as I can tell, I should’ve found all of these in the standard library (regex search with `` r"`\]`|`\[`" ``) \[except for in `core::convert` where I’ve noticed other things in the docs that I want to fix in a separate PR]. In particular, unlike #80733, I’ve added almost no new instance of inline code that’s broken up into multiple links (or some link and some link-free part). I also added tooltips (the stuff in quotes for the markdown link listings) in places that caught my eye, but that’s by no means systematic, just opportunistic. [Box]: https://doc.rust-lang.org/std/boxed/struct.Box.html "Box" [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html "Box" [Option]: https://doc.rust-lang.org/std/option/enum.Option.html "Option" [`Option`]: https://doc.rust-lang.org/std/option/enum.Option.html "Option" Context: I got annoyed by repeatedly running into new misformatted inline code while reading the standard library docs. I know that once issue #83997 (and/or related ones) are resolved, these changes become somewhat obsolete, but I fail to notice much progress on that end right now. r? `@jyn514`
2021-09-25Apply 16 commits (squashed)Frank Steffahn-1/+1
---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::fmt ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::{rc, sync} ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::string ---------- Fix spacing for links inside code blocks in alloc::vec ---------- Fix spacing for links inside code blocks in core::option ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in core::result ---------- Fix spacing for links inside code blocks in core::{iter::{self, iterator}, stream::stream, poll} ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{fs, path} ---------- Fix spacing for links inside code blocks in std::{collections, time} ---------- Fix spacing for links inside code blocks in and make formatting of `&str`-like types consistent in std::ffi::{c_str, os_str} ---------- Fix spacing for links inside code blocks, and improve link tooltips in std::ffi ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{io::{self, buffered::{bufreader, bufwriter}, cursor, util}, net::{self, addr}} ---------- Fix typo in link to `into` for `OsString` docs ---------- Remove tooltips that will probably become redundant in the future ---------- Apply suggestions from code review Replacing `…std/primitive.reference.html` paths with just `reference` Co-authored-by: Joshua Nelson <github@jyn.dev> ---------- Also replace `…std/primitive.reference.html` paths with just `reference` in `core::pin`
2021-09-17Stabilize `Iterator::map_while`Maybe Waffle-1/+1
2021-08-31Stabilize `Iterator::intersperse()`inquisitivecrystal-1/+1
2021-07-29Fix may not to appropriate might not or must notAli Malik-1/+1
2021-07-28Add TrustedRandomAccessNoCoerce supertrait without requirements or ↵Frank Steffahn-0/+2
guarantees about subtype coercions Update all the TrustedRandomAccess impls to also implement the new supertrait
2021-05-26Specialize implementationsJacob Pratt-0/+2
Implementations in stdlib are now optimized as they were before.
2021-03-27Add the tracking issue for `#![feature(iter_zip)]`Josh Stone-1/+1
2021-03-26Add function core::iter::zipJosh Stone-0/+2
This makes it a little easier to `zip` iterators: ```rust for (x, y) in zip(xs, ys) {} // vs. for (x, y) in xs.into_iter().zip(ys) {} ``` You can `zip(&mut xs, &ys)` for the conventional `iter_mut()` and `iter()`, respectively. This can also support arbitrary nesting, where it's easier to see the item layout than with arbitrary `zip` chains: ```rust for ((x, y), z) in zip(zip(xs, ys), zs) {} for (x, (y, z)) in zip(xs, zip(ys, zs)) {} // vs. for ((x, y), z) in xs.into_iter().zip(ys).zip(xz) {} for (x, (y, z)) in xs.into_iter().zip((ys.into_iter().zip(xz)) {} ``` It may also format more nicely, especially when the first iterator is a longer chain of methods -- for example: ```rust iter::zip( trait_ref.substs.types().skip(1), impl_trait_ref.substs.types().skip(1), ) // vs. trait_ref .substs .types() .skip(1) .zip(impl_trait_ref.substs.types().skip(1)) ``` This replaces the tuple-pair `IntoIterator` in rust-lang/rust#78204. There is prior art for the utility of this in [`itertools::zip`]. [`itertools::zip`]: https://docs.rs/itertools/0.10.0/itertools/fn.zip.html
2020-12-31Add Iterator::intersperse_withLukas Lueg-2/+2
2020-12-30Add tracking issueCamelid-1/+1
2020-12-30Add Iterator::intersperseJonas Schievink-0/+2
2020-11-23[update patch]William Chargin-3/+5
wchargin-branch: doc-iter-by-reference wchargin-source: e4069ac9a9d73860467cea74cf3ae1605af37d74
2020-11-23std::iter: document iteration over `&T` and `&mut T`William Chargin-0/+43
A colleague of mine is new to Rust, and mentioned that it was “slightly confusing” to figure out what `&mut` does in iterating over `&mut foo`: ```rust for value in &mut self.my_vec { // ... } ``` My colleague had read the `std::iter` docs and not found the answer there. There is a brief section at the top about “the three forms of iteration”, which mentions `iter_mut`, but it doesn’t cover the purpose of `&mut coll` for a collection `coll`. This patch adds an explanatory section to the docs. I opted to create a new section so that it can appear after the note that `impl<I: Iterator> IntoIterator for I`, and it’s nice for the existing “three forms of iteration” to appear near the top. Implementation note: I haven’t linkified the references to `HashSet` and `HashMap`, since those are in `std` and these docs are in `core`; linkifying them gave an “unresolved link” rustdoc error. Test Plan: Ran `./x.py doc library/core`, and the result looked good. Manually copy-pasted the two doctests into the playground and ran them. wchargin-branch: doc-iter-by-reference wchargin-source: 0f35369a8a735868621166608797744e97536792
2020-11-22Merge `use`s in core::iterWaffle-13/+10
2020-09-03pacify tidyThe8472-2/+3
2020-09-03impl TrustedRandomAccess for vec::IntoIterThe8472-1/+3