Absolute value function in Rust

2k views Asked by At

In this elementary Rust program a function calculates the absolute value of an integer, and main() helps in completing a statement with the result:

fn main() {
    let value = abs(-4);
    println!("{}.", value);
}

fn abs(x: i32) -> i32 {
    print!("The abs value of {} is ", x);

    if x > 0 {
        return x;
    } else {
        -x
    }
}

Is there a way to print correctly the whole statement "The abs value of... is..." into the abs() function? I tried unsuccessfully with

println!("The abs value of {} is {} ", x, x);

This always prints the value of the x parameter (e.g. -4, -4) so it's not correct.

And with

println!("The abs value of {} is {} ", x, abs(x));

But here, for some reason, Rust is not happy with recursion, gives a warning at compilation and then doesn't run the program.

2

There are 2 answers

2
wasmup On BEST ANSWER

Try this to avoid recursion:

fn main() {
    let value = abs(-4);
    println!("{}.", value);
}
fn abs(x: i32) -> i32 {
    let y = if x >= 0 { x } else { -x };
    println!("The abs value of {} is {} ", x, y);
    y
}

Output:

The abs value of -4 is 4
4.

There are built-in .abs() method for primitive types e.g. i8, i16, i32, i64, i128, f32, and f64:

assert_eq!(10i32.abs(), 10);
assert_eq!((-10i32).abs(), 10);

Overflow behavior

The absolute value of i32::min_value() cannot be represented as an i32, and attempting to calculate it will cause an overflow. This means that code in debug mode will trigger a panic on this case and optimized code will return i32::min_value() without a panic.

The following code, will panic in debug mode (and returns -128 in release mode):

fn main() {
    let a = -128_i8;
    println!("{}", a); // -128
    let b = a.abs();
    println!("{}", b); // -128
}

Since abs(-2_147_483_648_i32) is 2_147_483_648_u32, you may return u32 instead of i32:

fn abs(x: i32) -> u32 {
    if x >= 0 {
        x as u32
    } else if x == std::i32::MIN {
        2_147_483_648_u32
    } else {
        -x as u32
    }
}
fn main() {
    let value = abs(std::i32::MIN); // i32::min_value() // -2_147_483_648i32
    println!("{}.", value); // 2147483648
}

Output:

2147483648
0
Shepmaster On

The absolute value method is already defined; you do not need to implement it yourself

fn main() {
    let value = abs(-4);
    println!("{}.", value);
}

fn abs(x: i32) -> i32 {
    let val = x.abs();
    println!("The abs value of {} is {}", x, val);
    val
}