From 497a667f49c8dea870a8a61ddabd95e24765a80d Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 17 Apr 2026 09:45:13 +0800 Subject: Apply clippy suggestions - Use `BTreeMap::entry` API in `update_item` - Replace `or_insert_with(Vec::new)` with `or_default` - Iterate over map values instead of key-value pairs - Use `std::io::Error::other` for custom errors - Simplify filter logic with direct predicate - Replace manual `Into` implementation with `From` - Remove unnecessary explicit returns --- src/calc.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/calc.rs') diff --git a/src/calc.rs b/src/calc.rs index fcd0f5b..d1209c3 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -27,7 +27,7 @@ pub fn calculate_from(item: Bills) -> Result { } fn precheck(item: &Bills) -> Result<(), BillSplitError> { - for (_, bill_item) in &item.items { + for bill_item in item.items.values() { // Check if the paid amount is negative if bill_item.paid < 0.0 { return Err(BillSplitError::NegativePaidAmount); @@ -44,16 +44,14 @@ fn precheck(item: &Bills) -> Result<(), BillSplitError> { Ok(()) } -fn calculate_balances_and_transactions( - item: &Bills, -) -> ( - BTreeMap<(Who, Who), f64>, - BTreeMap>, -) { +type DirectTransactions = BTreeMap<(Who, Who), f64>; +type ItemsByWho = BTreeMap>; + +fn calculate_balances_and_transactions(item: &Bills) -> (DirectTransactions, ItemsByWho) { let mut direct_transactions: BTreeMap<(Who, Who), f64> = BTreeMap::new(); let mut items: BTreeMap> = BTreeMap::new(); - for (_, bill_item) in &item.items { + for bill_item in item.items.values() { let who_paid = &bill_item.who_paid; let paid = bill_item.paid; let split_count = bill_item.split.len() as f64; @@ -82,7 +80,7 @@ fn calculate_balances_and_transactions( items .entry(person.clone()) - .or_insert_with(Vec::new) + .or_default() .push(bill_result_item); } } -- cgit