Skip to content

Commit

Permalink
Document Sum::sum returns additive identities for []
Browse files Browse the repository at this point in the history
Because the neutral element of `<fNN as iter::Sum>` was changed to
`neg_zero`, the documentation needed to be updated, as it was reporting
inadequate information about what should be expected from the return.

Co-authored-by: Jubilee <workingjubilee@gmail.com>
  • Loading branch information
2 people authored and gitbot committed Feb 20, 2025
1 parent caa8321 commit fa42c72
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,8 @@ pub trait Iterator {
///
/// Takes each element, adds them together, and returns the result.
///
/// An empty iterator returns the zero value of the type.
/// An empty iterator returns the *additive identity* ("zero") of the type,
/// which is `0` for integers and `-0.0` for floats.
///
/// `sum()` can be used to sum any type implementing [`Sum`][`core::iter::Sum`],
/// including [`Option`][`Option::sum`] and [`Result`][`Result::sum`].
Expand All @@ -3511,6 +3512,10 @@ pub trait Iterator {
/// let sum: i32 = a.iter().sum();
///
/// assert_eq!(sum, 6);
///
/// let b: Vec<f32> = vec![];
/// let sum: f32 = b.iter().sum();
/// assert_eq!(sum, -0.0_f32);
/// ```
#[stable(feature = "iter_arith", since = "1.11.0")]
fn sum<S>(self) -> S
Expand Down

0 comments on commit fa42c72

Please sign in to comment.