Trusted answers to developer questions

What is the MAX constant in Rust?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Overview

All integer types in Rust, signed and unsigned, have their maximum value. We can get this value for any integer type with the MAX constant.

Syntax

type::MAX
Syntax for MAX constant in Rust

Parameters

type: This represents any integer datatypes, such as u32 , u8, i64, and so on.

Example

fn main() {
println!("{}", u8::MAX);
println!("{}", u16::MAX);
println!("{}", u32::MAX);
println!("{}", u64::MAX);
println!("{}", u128::MAX);
println!("{}", i8::MAX);
println!("{}", i16::MAX);
println!("{}", i32::MAX);
println!("{}", i64::MAX);
println!("{}", i128::MAX);
}

Explanation

  • Lines 2–11: We use the MAX method to get the largest value representable by an integer datatype. Next, we print the result to the console.

RELATED TAGS

rust
Did you find this helpful?