Skip to content
← All examples

Account withdrawal

Rust Classes & methods

impl + &mut self

Flowchart (ISO 5807)

YesNoStartInput amountamount > self.balanceOutput «Недостатньо коштів»Return falseEndself.balance -= amountReturn trueEndFigure 1 — Account::withdraw

Source code

struct Account {
    balance: f64,
}

impl Account {
    fn withdraw(&mut self, amount: f64) -> bool {
        if amount > self.balance {
            println!("Недостатньо коштів");
            return false;
        }
        self.balance -= amount;
        true
    }
}