Start learning free

LESSON 86 OF 348 · CONDITIONAL STATEMENTS - CHALLENGES

Recreating the abs() function in Rust

Practice

The abs() function returns the absolute value of a number. This means that it returns a number's positive value. You can think of it as the distance away from zero.

Create your own function abs() that recreates this functionality.

Examples:

abs(-1.217197940) ➞ 1.21719794

abs(-544.0) ➞ 544

abs(250.3) ➞ 250.3

abs(0) ➞ 0

abs(-0.14) ➞ 0.14

Note:

Do not use the abs() function provided by Rust (it will defeat the purpose of the challenge)


Hints:


You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is positive.
STEP 1 OF 3
    Ctrl + Enter to check

    Recreating Abs in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free