Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Rust Math


Share

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 number
  • floor() rounds down
  • ceil() 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

MethodDescriptionWorks on
max(x)Returns the highest of two numbersIntegers and decimal numbers
min(x)Returns the lowest of two numbersIntegers and decimal numbers
abs()Returns the absolute (positive) valueSigned integers (like i32) and decimal numbers
pow(n)Raises the number to the power of nIntegers
powi(n)Raises the number to the power of a whole number nDecimal numbers
powf(n)Raises the number to the power of a decimal number nDecimal numbers
sqrt()Returns the square rootDecimal numbers
round()Rounds to the nearest whole numberDecimal numbers
floor()Rounds downDecimal numbers
ceil()Rounds upDecimal numbers


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.