Introduction
Functions are the primary way to organize reusable logic in Rust. Every Rust program starts with a main function, and additional functions can be defined anywhere in a file using the fn keyword. Rust functions use snake_case naming by convention, and the compiler will even warn you if you deviate from it.
Cricket analogy: The main function is like the toss that starts every match, and fn lets you define extra functions like calculate_run_rate anywhere in the file, with the compiler nudging you toward snake_case names like net_run_rate.
Syntax
fn function_name(param1: Type1, param2: Type2) -> ReturnType {
// function body
// last expression (no semicolon) is returned
}Explanation
Parameters must have explicit type annotations, and the return type is declared after an arrow (->). If a function returns a value, the last line of the body is typically an expression without a trailing semicolon; Rust implicitly returns that expression's value. Adding a semicolon turns the expression into a statement, which evaluates to the unit type () instead. You can also use the return keyword to exit a function early, which is useful inside conditionals or loops.
Cricket analogy: A calculate_strike_rate function declares runs: u32 with an explicit type and -> f64 for its return; the last line, runs as f64 / balls as f64 with no semicolon, is implicitly returned as the strike rate.
Example
fn add(a: i32, b: i32) -> i32 {
a + b // no semicolon: this is the return value
}
fn describe(n: i32) -> &'static str {
if n < 0 {
return "negative"; // early return
}
"non-negative"
}
fn main() {
let sum = add(3, 4);
println!("sum = {}", sum);
println!("7 is {}", describe(7));
println!("-2 is {}", describe(-2));
}Output
sum = 7
7 is non-negative
-2 is negativeKey Takeaways
- Functions are declared with fn and use snake_case names by convention.
- Parameter types and return types must be explicitly annotated.
- The last expression without a semicolon is implicitly returned.
- Adding a semicolon turns an expression into a statement that returns ().
- The return keyword allows early exits from a function body.
Practice what you learned
1. What determines the return value of a Rust function without using the return keyword?
2. What happens if you add a semicolon after the last expression in a function meant to return a value?
3. Which keyword allows a function to exit before reaching its final expression?
4. What is the idiomatic naming convention for Rust function names?
5. In `fn add(a: i32, b: i32) -> i32 { a + b }`, what does `-> i32` specify?
Was this page helpful?
You May Also Like
Closures in Rust
Understand Rust closures, their capture modes, and the Fn, FnMut, and FnOnce traits that govern how they use captured variables.
Higher-Order Functions and Iterators in Rust
Explore functions that take or return closures, the Iterator trait, and lazy adapters like map, filter, and fold.
Methods and impl Blocks in Rust
Learn how to attach behavior to structs and enums using impl blocks, self receivers, and associated functions.
if let and while let in Rust
Simplify single-pattern matching in Rust using the concise if let and while let constructs.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics