Skip to content

Add description of fold function arguments. #22976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,20 +607,28 @@ pub trait IteratorExt: Iterator + Sized {
/// Performs a fold operation over the entire iterator, returning the
/// eventual state at the end of the iteration.
///
/// # Arguments
Copy link
Member

Choose a reason for hiding this comment

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

We don't currently have any conventions for using Arguments as a top-level thing.

///
/// * `init` - initial value for accumulator, it is also returned if iterator is empty
/// * `func(acc, item)` - function applied to the elements
/// + `acc` - accumulator = value returned by last call of `func` (or `init` if it is first
/// call)
/// + `item` - current element
///
/// # Examples
///
/// ```
/// let a = [1, 2, 3, 4, 5];
/// assert!(a.iter().fold(0, |a, &b| a + b) == 15);
/// assert!(a.iter().fold(0, |acc, &item| acc + item) == 15);
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a good change 👍

/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn fold<B, F>(self, init: B, mut f: F) -> B where
fn fold<B, F>(self, init: B, mut func: F) -> B where
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this, as f is pretty standard for a function argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made this change because for me it seems nicer in docs and IMHO is a little bit more descriptive. Anyway it is just cosmetic and if you want I will revert it.

F: FnMut(B, Self::Item) -> B,
{
let mut accum = init;
for x in self {
accum = f(accum, x);
accum = func(accum, x);
}
accum
}
Expand Down