-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
/// | ||
/// * `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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this, as There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
There was a problem hiding this comment.
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.