From 576d9db1b5b12c934a5542b7ff8290a5f0b7ab56 Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Fri, 13 Nov 2026 12:04:59 -0600 Subject: fix: reimplement transfer --- learn_3/src/main.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/learn_3/src/main.rs b/learn_3/src/main.rs index c588672..32e5c2b 100644 --- a/learn_3/src/main.rs +++ b/learn_3/src/main.rs @@ -31,26 +31,38 @@ impl Bank { } pub fn transfer(&mut self, from: u32, to: u32, amount: i64) -> bool { - if from == to || amount < 1 || self.get(to).is_none() { + if from == to { return false; } - let from_account = match self.get_mut(from) { + if amount < 1 { + return false; + } + + let from_read = match self.get(from) { Some(account) => account, _ => return false, }; - if from_account.balance < amount { + if from_read.balance - amount < 0 { return false; } - from_account.balance -= amount; - let to_account = match self.get_mut(to) { + + let to_read = match self.get(to) { Some(account) => account, _ => return false, }; - if let Some(new_balance) = to_account.balance.checked_add(amount) { - to_account.balance += amount; - return true + if to_read.balance.checked_add(amount).is_none() { + return false; } - false + + let Some(from_write) = self.get_mut(from) else { + return false; + }; + from_write.balance -= amount; + let Some(to_write) = self.get_mut(to) else { + return false; + }; + to_write.balance += amount; + true } } @@ -147,7 +159,7 @@ mod tests { let maxed_id: u32 = 3; bank.add(Account{ - id:to_id, + id:maxed_id, balance:i64::MAX, }); -- cgit v1.2.3