Functions Have Explicit Return Types (No Guesswork)
Expression-Based Functions (No return Needed)
Strong Type Checking at Compile Time
Ownership & Borrowing in Function Parameters
No Null Parameters (Option)
Error Handling Built into Function Signatures
Pattern Matching with Function Results
Generic Functions with Zero Runtime Cost
Immutable by Default (Safer Functions)
Fearless Concurrency in Functions
Functions Have Explicit Return Types (No Guesswork)
fn add(a: i32, b: i32) -> i32 {
a + b
}
Why this is better
The return type is always known at compile time
No hidden or dynamic return values
Other languages
Python, PHP → return types optional or dynamic
JavaScript → return type inferred at runtime
👉 Rust makes function behavior predictable and safe
Expression-Based Functions (No return Needed)
fn square(x: i32) -> i32 {
x * x
}
Benefits
Cleaner syntax
Fewer mistakes
Encourages functional programming style
Other languages
Java, C++ → return mandatory
PHP → explicit return required
👉 Rust reduces boilerplate while staying safe
3️⃣
Strong Type Checking at Compile Time
fn multiply(a: i32, b: i32) -> i32 {
a * b
}
// multiply(2, "3"); ❌ compile-time error
Why it matters
Prevents invalid function calls
Eliminates runtime type errors
Other languages
Python, PHP → errors at runtime
JavaScript → implicit coercions
👉 Rust catches bugs before execution
4️⃣
Ownership & Borrowing in Function Parameters
fn print_name(name: &String) {
println!("{}", name);
}
Advantages
No accidental copies
No dangling references
Memory safety guaranteed
Other languages
C/C++ → unsafe pointers
Java → object references with GC
PHP/Python → implicit heap allocations
👉 Rust functions control memory precisely
5️⃣
No Null Parameters (Option)
fn find_user(id: Option<u32>) {
match id {
Some(i) => println!("User {}", i),
None => println!("No user"),
}
}
Benefits
Null safety enforced
All cases handled explicitly
Other languages
Java, PHP → NullPointerException risk
Go → nil ambiguity
👉 Rust eliminates null-related crashes
6️⃣
Error Handling Built into Function Signatures
fn read_file() -> Result<String, std::io::Error> {
// ...
}
Why this is powerful
Caller must handle success or failure
Errors are part of API design
Other languages
Java → unchecked exceptions
PHP → mixed error handling
Python → runtime exceptions
👉 Rust makes errors visible and unavoidable
7️⃣
Pattern Matching with Function Results
match read_file() {
Ok(data) => println!("{}", data),
Err(e) => eprintln!("{}", e),
}
Advantages
Exhaustive handling
No silent failures
👉 Backend logic becomes robust and explicit
8️⃣
Generic Functions with Zero Runtime Cost
fn max<T: Ord>(a: T, b: T) -> T {
if a > b { a } else { b }
}
Benefits
Code reuse
No runtime polymorphism
No performance penalty
Other languages
Java → type erasure
Python → runtime generics
👉 Rust generics are zero-cost abstractions
9️⃣
Immutable by Default (Safer Functions)
fn increment(x: i32) -> i32 {
x + 1
}
Why this matters
Prevents side effects
Easier reasoning
Better concurrency
Other languages
Mutable by default (Java, PHP)
👉 Rust functions are side-effect conscious
🔟
Fearless Concurrency in Functions
use std::thread;
fn spawn_worker(data: Vec<i32>) {
thread::spawn(move || {
println!("{:?}", data);
});
}
Rust guarantees
Thread safety
No data races
Other languages
Java → locks
Go → race-prone goroutines
PHP → limited concurrency
👉 Rust functions scale safely
Top comments (0)