Skip to content

Improve Iterator::by_ref example - #80805

Merged
bors merged 4 commits into
rust-lang:masterfrom
camelid:iter-by_ref-example
Apr 23, 2021
Merged

Improve Iterator::by_ref example#80805
bors merged 4 commits into
rust-lang:masterfrom
camelid:iter-by_ref-example

Conversation

@camelid

Copy link
Copy Markdown
Member

I split the example into two: one that fails to compile, and one that
works. I also made them identical except for the addition of by_ref
so we don't confuse readers with random differences.

cc @steveklabnik, who is the one that added the previous version of this example

I split the example into two: one that fails to compile, and one that
works. I also made them identical except for the addition of `by_ref`
so we don't confuse readers with random differences.
@camelidcamelid added A-iterators Area: Iterators C-cleanup Category: PRs that clean code up or issues documenting cleanup. C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs-api [DEPRECATED; DO NOT USE] labels Jan 8, 2021
@rust-highfive

Copy link
Copy Markdown
Contributor

r? @withoutboats

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 8, 2021
@camelid

Copy link
Copy Markdown
MemberAuthor

Might as well just r? @steveklabnik

Comment threadlibrary/core/src/iter/traits/iterator.rs Outdated

@steveklabniksteveklabnik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I think some of this is excellent, some of it feels like churn, and some of it moves us away from standard constructs.

Comment threadlibrary/core/src/iter/traits/iterator.rs Outdated
Comment threadlibrary/core/src/iter/traits/iterator.rs Outdated
Comment threadlibrary/core/src/iter/traits/iterator.rs Outdated
@camelidcamelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 21, 2021
@camelidcamelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 8, 2021
Comment on lines 1553 to 1579
/// This demonstrates a use case that needs `by_ref`:
///
/// ```compile_fail,E0382
/// let a = [1, 2, 3, 4, 5];
/// let mut iter = a.iter();
///
/// let sum: i32 = iter.take(3).fold(0, |acc, i| acc + i);
/// assert_eq!(sum, 6);
///
/// // if we try to use iter again, it won't work. The following line
/// // gives "error: use of moved value: `iter`
/// // assert_eq!(iter.next(), None);
/// // Error! We can't use `iter` again because it was moved
/// // by `take`.
/// assert_eq!(iter.next(), Some(&4));
/// ```
///
/// // let's try that again
/// let a = [1, 2, 3];
/// Now, let's use `by_ref` to make this work:
///
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut iter = a.iter();
///
/// // instead, we add in a .by_ref()
/// let sum: i32 = iter.by_ref().take(2).fold(0, |acc, i| acc + i);
///
/// assert_eq!(sum, 3);
/// // We add in a call to `by_ref` here so `iter` isn't moved.
/// let sum: i32 = iter.by_ref().take(3).fold(0, |acc, i| acc + i);
/// assert_eq!(sum, 6);
///
/// // now this is just fine:
/// assert_eq!(iter.next(), Some(&3));
/// assert_eq!(iter.next(), None);
/// // And now we can use `iter` again because we still own it.
/// assert_eq!(iter.next(), Some(&4));
/// ```

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, should I just remove the later example? I don't feel like it adds much that the basic usage example doesn't show.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camelid do youw ant to update this without the last example? looks good enough to me without it

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dylan-DPC OK, I removed the example. Do you want to review this now?

@camelidcamelid removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 14, 2021
@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 1, 2021
@crlf0710crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 20, 2021
@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 4, 2021
@Dylan-DPC-zz

Copy link
Copy Markdown

i could review it, but i would prefer @steveklabnik to have a final look at it

@joshtriplett

Copy link
Copy Markdown
Member

This looks reasonable to me.

@steveklabnik

Copy link
Copy Markdown
Contributor

Looks good to me, thanks!

@bors: r+ rollup

@bors

bors commented Apr 23, 2021

Copy link
Copy Markdown
Collaborator

📌 Commit 49ccc3f has been approved by steveklabnik

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2021
@JohnTitorJohnTitor mentioned this pull request Apr 23, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 23, 2021
Rollup of 11 pull requests
Successful merges:
- rust-lang#80805 (Improve `Iterator::by_ref` example)
- rust-lang#84248 (Remove duplicated fn(Box<[T]>) -> Vec<T>)
- rust-lang#84321 (rustdoc: Convert sub-variant toggle to HTML)
- rust-lang#84359 (:arrow_up: rust-analyzer)
- rust-lang#84374 (Clean up .gitignore)
- rust-lang#84387 (Move `sys_common::poison` to `sync::poison`)
- rust-lang#84430 (doc/platform-support: clarify UEFI support)
- rust-lang#84433 (Prevent control, shift and alt keys to make search input lose focus)
- rust-lang#84444 (doc: Get rid of "[+] show undocumented items" toggle on numeric From impls)
- rust-lang#84456 (Fix ICE if original_span(fn_sig) returns a span not in body sourcefile)
- rust-lang#84469 (Update comment on `PrimTy::name_str`)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit dcb4083 into rust-lang:masterApr 23, 2021
@rustbotrustbot added this to the 1.53.0 milestone Apr 23, 2021
@camelid
camelid deleted the iter-by_ref-example branch April 24, 2021 02:14
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-iteratorsArea: IteratorsC-cleanupCategory: PRs that clean code up or issues documenting cleanup.C-enhancementCategory: An issue proposing an enhancement or a PR with one.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@camelid@rust-highfive@Dylan-DPC-zz@joshtriplett@steveklabnik@bors@crlf0710@JohnCSimon@withoutboats@rustbot