Skip to content

Commit c309089

Browse files
Rollup merge of #136710 - JakenHerman:jaken/iterator-docs, r=workingjubilee
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. Relevant Commit: 4908188 Relevant Pull Request: #129321 --- The referenced commit causes unintended side effects on presentation layer applications like using Tera templates, for example. I'm not sure what the motivation was behind the original change, but it seems like more discussion should be put into this issue and potentially have that change reverted.
2 parents 5e4d627 + 4457f44 commit c309089

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

library/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)