Start learning free

LESSON 75 OF 414 · CONDITIONAL STATEMENTS

Is Number Negative in Rust: if and else

Practice Read the full explanation

Create a function that takes a number n as its only argument and prints "negative" to the console if n is less than zero, otherwise prints "positive" to the console.

Example 1:

Input: n = 5
Output: "positive"
Explanation:
5 is greater than or equal to zero, so our function prints "positive" to the console.

Example 2:

Input: n = 0
Output: "positive"
Explanation:
0 is greater than or equal to zero, so our function prints "positive" to the console.

Example 3:

Input: n = -2
Output: "negative"
Explanation:
-2 is less than zero, so our function prints "negative" to the console.

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

    RUST · CONDITIONAL STATEMENTS

    Is Number Negative in Rust, explained

    Problem overview

    The function is_negative receives an integer n and prints one word. It prints negative when n is less than zero and positive otherwise. It returns nothing, so the answer is the text it prints, not a returned value.

    Three examples show the rule. For n = 5 the output is positive. For n = -2 the output is negative. For n = 0, the output is positive: zero is not less than zero, so it goes with the non-negative numbers, even though a mathematician wouldn't call it positive. The lesson's wording makes the rule simple: anything that is not negative prints positive.

    Approach

    There are exactly two outcomes, and every integer falls into exactly one of them. That is the situation if-else is made for. The comparison n < 0 is true for negative numbers and false for everything else, so one condition and one else cover all inputs.

    You don't need a second check for "is it positive?". Adding one, such as n > 0, would leave zero matching neither branch, and zero would print nothing. Letting else take every remaining value avoids that gap.

    Step by step

    1. Write the if with its condition. The condition is n < 0. Rust needs no parentheses, and the block is delimited by braces. At first the block can be empty.
    2. Print the negative case. Inside the block, call println!("negative"). The exclamation mark shows that println! is a macro. It adds a line break after the text.
    3. Add the else. After the closing brace of the if block, write else with its own block and print positive there. It runs for zero and for every positive number, since those are the values where n < 0 is false.

    The solution in Rust

    impl Solution {
        fn is_negative(n: i32) {
            if n < 0 {
                println!("negative");
            } else {
                println!("positive");
            }
        }
    }
    

    A few Rust details in this code:

    • No return type. The signature has no ->, so the function returns the unit type (), Rust's "nothing". The function's job is its side effect, printing.
    • The type of n. i32 is a 32-bit signed integer, so it can hold negative values. An unsigned type such as u32 never could, and the question would be pointless.
    • Both branches print. Because each branch ends with a statement ending in ;, the if-else is itself of type (), which matches the function's return type.
    • String literals. "negative" and "positive" are plain text with no quotes in the output. The tests compare what is printed, so the words must be spelled exactly, in lowercase.
    • The impl Solution block. The function lives inside it as a method without self, so it is called as Solution::is_negative(n).

    Edge cases and common mistakes

    • Zero. 0 < 0 is false, so zero prints positive. Writing n <= 0 would make zero print negative, which is wrong here.
    • Printing the quotes. The examples show "positive" in quotes to mark it as text. The program prints the word without them.
    • Returning instead of printing. The function has no return type, and the task asks for printed output. Returning a string would not compile with this signature.
    • A missing else. With only the if, non-negative inputs print nothing.
    • Using print!. print! doesn't add the line break, so the output would not end its line.
    • The smallest integer. i32::MIN is negative, so it prints negative. No arithmetic is involved, so there is no overflow to worry about.

    Testing

    • n = 5 prints positive.
    • n = 0 prints positive.
    • n = -2 prints negative.
    • n = -1 prints negative.
    • n = 2147483647 prints positive.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free