Rust - Data Types: Operations

less than 1 minute read


:information_source: Note: Read the offical docs to get the most updated information.

Type Operations

Arithmetics

Basic mathematical operations you’d expect for all the number types:

fn main() {
    // addition
    let sum = 5 + 10;

    // subtraction
    let difference = 95.5 - 4.3;

    // multiplication
    let product = 4 * 30;

    // division
    let quotient = 56.7 / 32.2;
    let truncated = -5 / 3; // Results in -1
    // Integer division truncates toward zero to the nearest integer

    // remainder
    let remainder = 43 % 5;
}

Appendix B contains a list of all operators that Rust provides.

Updated:

Leave a Comment