Skip to content

Commit 4e83e68

Browse files
JakenHermanworkingjubilee
authored and
gitbot
committed
Document Sum::sum returns additive identities for []
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 <[email protected]>
1 parent c5bf9b8 commit 4e83e68

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

core/src/iter/traits/iterator.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3493,7 +3493,8 @@ pub trait Iterator {
34933493
///
34943494
/// Takes each element, adds them together, and returns the result.
34953495
///
3496-
/// An empty iterator returns the zero value of the type.
3496+
/// An empty iterator returns the *additive identity* ("zero") of the type,
3497+
/// which is `0` for integers and `-0.0` for floats.
34973498
///
34983499
/// `sum()` can be used to sum any type implementing [`Sum`][`core::iter::Sum`],
34993500
/// including [`Option`][`Option::sum`] and [`Result`][`Result::sum`].
@@ -3511,6 +3512,10 @@ pub trait Iterator {
35113512
/// let sum: i32 = a.iter().sum();
35123513
///
35133514
/// assert_eq!(sum, 6);
3515+
///
3516+
/// let b: Vec<f32> = vec![];
3517+
/// let sum: f32 = b.iter().sum();
3518+
/// assert_eq!(sum, -0.0_f32);
35143519
/// ```
35153520
#[stable(feature = "iter_arith", since = "1.11.0")]
35163521
fn sum<S>(self) -> S

0 commit comments

Comments
 (0)