Rust Functions — Function Definitions, Parameters, Return Values, and Expressions
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you will learn about Rust Functions. We cover key concepts, practical examples, and best practices to help you master this topic.
Rust functions use fn keyword with typed parameters, return values with -> syntax, expressions without semicolons, and early returns with return keyword.
What You'll Learn
- Function definition syntax
- Parameters and return types
- Expressions vs statements
- Function overloading (not supported)
Why It Matters
Functions structure Rust programs. DodaZIP uses functions for pipeline stages. Rust's expression-based syntax enables concise functional patterns.
Real-World Use
Mathematical computations, data transformations, API handler functions, pipeline stages.
fn add(x: i32, y: i32) -> i32 {
x + y // Expression, no semicolon = return value
}
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
fn divide(a: f64, b: f64) -> Option<f64> {
if b == 0.0 {
None // Early return
} else {
Some(a / b)
}
}
fn main() {
println!("Sum: {}", add(5, 3));
println!("{}", greet("World"));
match divide(10.0, 2.0) {
Some(result) => println!("Quotient: {}", result),
None => println!("Cannot divide by zero"),
}
}
← Previous
Rust Data Types — Scalar Types, Compound Types, Type Inference, and Casting
Next →
Rust Control Flow — If/Else, Loops (loop, while, for), and Match Statements
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro