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

Numerical operations on integers

  • /

  • %

  • The division keeps the type so dividing one integer by another integer will always return an integer.

fn main() {
    let x = 23;
    let y = 19;
    println!("{x}");
    println!("{y}");

    let add = x + y;
    println!("add: {add}");

    let multiple = x * y;
    println!("multiple: {multiple}");

    let neg = -x;
    println!("neg: {neg}");

    let diff = y - x;
    println!("diff: {diff}");

    let div = x / y;
    println!("div: {div}");

    let modulus = x % y;
    println!("mod: {modulus}");
}
23
19
add: 42
multiple: 437
neg: -23
diff: -4
div: 1
mod: 4