Start learning free

LESSON 138 OF 414 · LOOPS & CONDITIONALS

Print Positive and Negative in Rust: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes a vector nums as argument and for every number in the vector:

  • prints "negative" if that number is less than zero

  • or prints the number itself, if the number is greater than or equal to zero

So when we called the function for vec![2, -12, 0, 4, -5, 3], we expected it to print:

2
negative
0
4
negative
3

but it seems like we made some mistakes because when we run our code, it produces this output:

positive
2
-12
0
positive
4
-5
positive
3

Assignment:

Your task is to fix our loop such that it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    RUST · LOOPS & CONDITIONALS

    Buggy Code Print Positive Negative in Rust, explained

    What the code is supposed to do

    The function print_positive_and_negative goes through a vector of integers. For each number it should print the word negative if the number is less than zero, and the number itself otherwise. For [2, -12, 0, 4, -5, 3] the expected output is 2, negative, 0, 4, negative, 3, one item per line.

    The starting code compiles but prints something else:

    fn print_positive_and_negative(nums: Vec<i32>) {
        for number in nums {
            if number > 0 {
                println!("positive");
            }
            println!("{}", number);
        }
    }
    
    // Do not change this code:
    fn main_function() {
        print_positive_and_negative(vec![2, -12, 0, 4, -5, 3]);
    }
    

    It prints positive, 2, -12, 0, positive, 4, -5, positive, 3. There is an extra line before every positive number, and the negative numbers are printed as they are.

    What goes wrong

    Bug 1: the wrong condition and the wrong word

    The if catches numbers greater than zero and prints positive. But the special message belongs to the numbers below zero, and it should say negative. The test is on the wrong side of zero, and the word is wrong.

    Bug 2: the number is always printed

    The last println! is outside the if, so it runs for every number. A number that got a message also gets printed afterwards, which is why 2 produces two lines. The task wants exactly one line per number: the word or the number, never both.

    A choice between two outputs, where exactly one must happen, is what else is for.

    Fixing it step by step

    1. Flip the condition to number < 0, so the branch catches negative numbers only.
    2. Change the message to "negative".
    3. Move the number print into an else. It now runs only when the number is not negative, that is, for zero and positive numbers.

    The fixed code in Rust

    fn print_positive_and_negative(nums: Vec<i32>) {
        for number in nums {
            if number < 0 {
                println!("negative");
            } else {
                println!("{}", number);
            }
        }
    }
    
    fn main_function() {
        print_positive_and_negative(vec![2, -12, 0, 4, -5, 3]);
    }
    

    Trace the example: 2 is not negative, so 2 is printed. -12 is negative, so the word is printed. 0 is not below zero, so 0 is printed. 4 is printed. -5 gives the word. 3 is printed. That is the expected output, with six lines for six numbers.

    Rust details

    • if/else chooses exactly one branch. Whatever the condition says, only one of the two blocks runs for each number.
    • Zero goes to the else branch. The condition is strictly less than zero, so 0 is printed as a number, as the expected output shows.
    • The condition has no parentheses. Rust requires a bool there and no brackets. A number alone, such as if number, is a compile error, since integers aren't converted to booleans.
    • Printing a value. println!("{}", number) puts the number where the placeholder is. A quoted word like "negative" is printed as written.
    • The vector is moved into the loop. for number in nums consumes the vector, and each number is a plain i32, so number < 0 needs no dereference.

    Common mistakes

    • Using <=. That would print the word for 0 too, but the task prints zero as a number.
    • Adding a second if instead of an else. Two separate tests could both fire or both miss. Using else guarantees exactly one line per number.
    • Fixing the word but not the structure. If the number print stays outside the if, each negative number appears as the word and then as the number.

    Testing

    • [2, -12, 0, 4, -5, 3] prints 2, negative, 0, 4, negative, 3.
    • [-1] prints negative.
    • [0] prints 0.
    • [] prints nothing.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free