Rust Math
Math
In the Operators chapter, you used +, -, *, / and % to do math.
For other common calculations, like finding the highest value or the square root of a number, Rust has built-in methods. A method is a function that belongs to a value. You call it with a dot after the value or variable, for example x.sqrt().
Highest and Lowest Value
The max() method returns the highest of two numbers, and min() returns the lowest:
Example
let a: i32 = 5;
let b: i32 = 10;
println!("{}", a.max(b));
println!("{}", a.min(b));
Try it Yourself »
Absolute Value
The abs() method returns the absolute (positive) value of a number:
Example
let temperature: i32 = -7;
println!("{}", temperature.abs());
Try it Yourself »
Square Root
The sqrt() method returns the square root of a decimal number:
Example
let x: f64 = 64.0;
println!("{}", x.sqrt());
Try it Yourself »
Power
To raise a number to a power, use pow() for integers and powi() for decimal numbers. base.pow(3) means base * base * base:
Example
let base: i32 = 2;
println!("{}", base.pow(3));
let side: f64 = 1.5;
println!("{}", side.powi(2));
Try it Yourself »
Rounding
Decimal numbers have three methods for rounding:
round()rounds to the nearest whole numberfloor()rounds downceil()rounds up
Example
let x: f64 = 4.6;
println!("{}", x.round());
println!("{}", x.floor());
println!("{}", x.ceil());
Try it Yourself »
Note: The results are still decimal numbers (f64), but Rust prints 5.0 as 5.
The PI Constant
Rust has the number PI (3.14159...) built in. To use it, write use std::f64::consts::PI; at the top of your program. This example finds the area of a circle:
Example
use std::f64::consts::PI;
fn main() {
let radius: f64 = 3.0;
let area = PI * radius * radius;
println!("{}", area);
}
Try it Yourself »
Write the Type
You might have noticed that every example on this page writes the type when it creates a variable, like let x: f64 = 64.0;. Without the type, Rust does not know which kind of number x is, so it cannot find the method:
Example
let x = 64.0;
println!("{}", x.sqrt()); // Error
Try it Yourself »
To fix it, write the type: let x: f64 = 64.0;
Note: Remember that dividing two integers drops the decimals: 7 / 2 is 3. Use decimal numbers (7.0 / 2.0) or type casting when you need the decimals.
Math Methods
| Method | Description | Works on |
|---|---|---|
max(x) | Returns the highest of two numbers | Integers and decimal numbers |
min(x) | Returns the lowest of two numbers | Integers and decimal numbers |
abs() | Returns the absolute (positive) value | Signed integers (like i32) and decimal numbers |
pow(n) | Raises the number to the power of n | Integers |
powi(n) | Raises the number to the power of a whole number n | Decimal numbers |
powf(n) | Raises the number to the power of a decimal number n | Decimal numbers |
sqrt() | Returns the square root | Decimal numbers |
round() | Rounds to the nearest whole number | Decimal numbers |
floor() | Rounds down | Decimal numbers |
ceil() | Rounds up | Decimal numbers |