Home »
Rust »
Rust Programs
Rust program to demonstrate the use of abs() function
Given a number, we have to find its absolute value using the abs() function.
Last Updated : September 28, 2021
Problem Statement
Here, we will read the number from the user. Then we will find the absolute value of the number and print the result.
Program/Source Code
The source code to demonstrate the use of abs() function is given below. The given program is compiled and executed successfully.
// Rust program to demonstrate the
// use of abs() function
fn main() {
let mut n1:i32 = -5;
let mut n2:i32 = 7;
let mut res:i32 = 0;
res = n1.abs();
println!("Absolute value is n1: {}",res);
res = n2.abs();
println!("Absolute value is n2: {}",res);
}
Output
Absolute value is n1: 5
Absolute value is n2: 7
Explanation
Here, we created two integer variables n1 and n2. Then we found the absolute value of n1 and n2 using the abs() function. After that, we printed the result.
Rust Basic Programs »
Advertisement
Advertisement