Prime Number Check
Easy
+2 pts
Algorithms
4/4
🎯 In Python, you might write a prime check like this:
def is_perfect_square(n):
if n < 0:
return False
root = int(n**0.5)
return root * root == n
Checking primality in Rust follows similar logic to what you'd write in Python, but introduces a few language features worth understanding.
Early return
So far you've seen Rust functions where the last expression is the return value. But sometimes you want to bail out early — that's what return is for:
fn check(n: u64) -> bool {
if n == 0 {
return false; // exit immediately
}
// ... more logic ...
true // last expression = default return
}
Both styles are idiomatic. Use expression-based returns when the function flows naturally to a …
Login to see the full exercise.
Topics