Problem overview
The function negative takes an integer n and returns it as a negative number. If n is positive, the answer is its opposite. If n is already negative, it comes back unchanged. Zero stays zero.
negative(4) -> -4
negative(15) -> -15
negative(-4) -> -4
negative(0) -> 0
This is easy to misread as "flip the sign". Flipping would turn -4 into 4, but the task asks for -4. The result is never positive.
Approach
Split the inputs into two groups by asking one question: is n less than zero?
- If yes, the number is already negative and is returned as it is.
- If no,
nis zero or positive, and the unary minus-ngives the negative value. For zero,-0is just0, so the zero example works without special handling.
In maths terms the result is minus the absolute value of n. The if-else spells out the same rule in steps.
Step by step
- Write the
if. Checkn < 0. A true result means the input is already negative. - Return
nfrom inside theif. No change is needed in that case. - Add the
elseand return-n. For zero or a positive number, putting a minus in front produces the negative value that is needed.
The solution in Rust
impl Solution {
fn negative(n: i32) -> i32 {
if n < 0 {
return n;
} else {
return -n;
}
}
}
The Rust details worth knowing:
- The signature.
n: i32is the parameter, and-> i32declares that the function returns ani32. Without the arrow, the function would return()and thereturnstatements would not compile. returnwith a semicolon. Each branch leaves the function immediately with its value. This is correct, and it is how the lesson writes it.- An expression-style alternative. Rust functions return the value of their last expression, so you can drop
returnand the semicolons:if n < 0 { n } else { -n }. Both branches produce ani32, so the wholeif-elseis the function's result. - Unary minus.
-nis allowed becausei32is signed. For an unsigned type such asu32it would not compile. - The one special input.
i32::MINis negative, so it takes theifbranch and returns as it is. Negating it would be a problem, because-i32::MINdoesn't fit in ani32: an optimised build wraps back to-2147483648, and a debug build panics. Theelsebranch only sees zero and positive numbers, whose negation always fits, so the function never negatesi32::MIN.
Edge cases and common mistakes
- Zero. It goes to the
elsebranch and returns-0, which is0for integers. Integers have no negative zero. - Negating everything. Returning
-nfor all inputs fails on negative numbers:-(-4)is4. - Using
n <= 0. It still gives correct answers, because negating zero gives zero, but the check is not needed. - Using
n > 0for the negation. That works too, as long as the other branch returnsn. What matters is that each input is handled by exactly one branch. - Forgetting the return type. Leaving out
-> i32gives a type error, since the function would be declared to return nothing. - Printing instead of returning. This function returns a value. The calling code reads the result, so printing it isn't enough.
Testing
n = 4gives-4.n = 15gives-15.n = -4gives-4.n = 0gives0.n = 1gives-1.
