Absolute value
The absolute value tells you how far a number is from zero.
i64::abs
Simple example
Start−3
Result3
fn main() {
println!("{}", i64::abs(-3));
}
3
Advertisements
Everyday examples
It is −6 degrees outside. How many degrees below zero is that?
Start−6 → 0
Result6
fn main() {
let temperature: i64 = -6;
println!("{}", i64::abs(temperature));
}
6
The target is 15 points and the score is 12. How far apart are they?
Start12 − 15
Result3
fn main() {
let score: i64 = 12;
let target: i64 = 15;
println!("{}", i64::abs((score - target)));
}
3
A lift goes from floor 5 to floor 2. How many floors does it travel?
Start5 → 2
Result3
fn main() {
let start: i64 = 5;
let finish: i64 = 2;
println!("{}", i64::abs((finish - start)));
}
3
Use small signed numbers here. The result is the distance from zero.