Skip to content

Commit b76172b

Browse files
RobWaltjswrenn
authored andcommitted
chore: adjust docs to reflect discussion in the PR
1 parent 955927f commit b76172b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,8 +2499,18 @@ pub trait Itertools: Iterator {
24992499
/// └─f─f─f─f─f─f
25002500
/// ```
25012501
///
2502-
/// If `f` is non-associative, prefer the normal [`Iterator::reduce`] instead to prevent
2503-
/// unexpected behavior unless you know what you're doing.
2502+
/// If `f` is associative you should also decide carefully:
2503+
///
2504+
/// - if `f` is a trivial operation like `u32::wrapping_add`, prefer the normal
2505+
/// [`Iterator::reduce`] instead since it will most likely result in the generation of simpler
2506+
/// code because the compiler is able to optimize it
2507+
/// - otherwise if `f` is non-trivial like `format!`, you should use `tree_fold1` since it
2508+
/// reduces the number of operations from `O(n)` to `O(ln(n))`
2509+
///
2510+
/// Here "non-trivial" means:
2511+
///
2512+
/// - any allocating operation
2513+
/// - any function that is a composition of many operations
25042514
///
25052515
/// ```
25062516
/// use itertools::Itertools;

0 commit comments

Comments
 (0)