Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

fn

fn-decl ::= attribute* 'pub'? 'fn' Ident generic-params? '(' param-list ')' '->' TypeExpr fn-body
fn-body ::= '=' expr ';'                          // single-expression
         |  '{' stmt* expr? '}'                    // block, implicit return
param   ::= 'self' | Ident ':' TypeExpr

self and Self. Argon is value-semantic; there is no reference or borrow form. self is always owned-by-value (no &self / &mut self) and is admitted only as the first parameter of a method inside an impl block or a trait declaration; its type is the enclosing impl’s target. Self resolves to the implementing type in trait declarations, to Type inside impl Trait for Type, and to Type inside bare impl Type { … }.

pub fn rent_per_day(l: Lease) -> Money = l.monthly_rent / 30;

pub fn classify(area: Decimal) -> Size {
    if area < 50.0 { Size::Small }
    else if area < 200.0 { Size::Medium }
    else { Size::Large }
}

pub fn safe_div(a: Money, b: Money) -> Result<Money, Diagnostic> {
    if b == 0 {
        Err(Diagnostic { severity: Severity::Error, code: "Math::DivZero", message: "..." })
    } else {
        Ok(a / b)
    }
}