diff options
| author | Jaken Herman <JakenHerman7@Gmail.com> | 2025-02-07 12:45:56 -0600 |
|---|---|---|
| committer | Jubilee Young <workingjubilee@gmail.com> | 2025-02-08 14:06:13 -0800 |
| commit | 4457f44065837198ba751ee1c3ff6beb0036bb4b (patch) | |
| tree | 6190f6692906a96d96cfd9ed7b94954a94290cc0 /library/core/src | |
| parent | 64e06c0f5578829373743884b708d494136c3e8f (diff) | |
| download | rust-4457f44065837198ba751ee1c3ff6beb0036bb4b.tar.gz rust-4457f44065837198ba751ee1c3ff6beb0036bb4b.zip | |
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 <workingjubilee@gmail.com>
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/iter/traits/iterator.rs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 91c3a4b29b5..42886e90f99 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -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`]. @@ -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 |
