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
- Write the
ifwith its condition. The condition isn < 0. Rust needs no parentheses, and the block is delimited by braces. At first the block can be empty. - Print the negative case. Inside the block, call
println!("negative"). The exclamation mark shows thatprintln!is a macro. It adds a line break after the text. - Add the
else. After the closing brace of theifblock, writeelsewith its own block and printpositivethere. It runs for zero and for every positive number, since those are the values wheren < 0is 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.i32is a 32-bit signed integer, so it can hold negative values. An unsigned type such asu32never could, and the question would be pointless. - Both branches print. Because each branch ends with a statement ending in
;, theif-elseis 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 Solutionblock. The function lives inside it as a method withoutself, so it is called asSolution::is_negative(n).
Edge cases and common mistakes
- Zero.
0 < 0is false, so zero printspositive. Writingn <= 0would make zero printnegative, 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 theif, 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::MINis negative, so it printsnegative. No arithmetic is involved, so there is no overflow to worry about.
Testing
n = 5printspositive.n = 0printspositive.n = -2printsnegative.n = -1printsnegative.n = 2147483647printspositive.
