What it does
Replaces instances of Iterator::fold which replicate the behavior of Iterator::try_fold
Lint Name
manual_try_fold
Category
complexity, perf
Advantage
Iterator::try_fold is more efficient, because it terminates early in the Err/None case
- Outsources error handling to
Iterator::try_fold
Drawbacks
Iterator::try_fold is newer than Iterator::fold, so may be unfamiliar to some users or not available on projects that want to work on old versions of Rust
Example
fn maybe_sum(xs: &[u32]) -> Option<u32> {
xs.iter().fold(Some(0), |sum, x| sum?.checked_add(*x))
}
Could be written as:
fn maybe_sum(xs: &[u32]) -> Option<u32> {
xs.iter().try_fold(0, |sum, x| sum.checked_add(*x))
}
What it does
Replaces instances of
Iterator::foldwhich replicate the behavior ofIterator::try_foldLint Name
manual_try_fold
Category
complexity, perf
Advantage
Iterator::try_foldis more efficient, because it terminates early in theErr/NonecaseIterator::try_foldDrawbacks
Iterator::try_foldis newer thanIterator::fold, so may be unfamiliar to some users or not available on projects that want to work on old versions of RustExample
Could be written as: