-
Book Overview & Buying
-
Table Of Contents
The Rust Programming Handbook
By :
Functions are a core component of Rust programming. They provide the means to organize your code into reusable blocks. Functions encapsulate logic, making your code more modular, maintainable, and easier to understand. We will explore more function in Chapter 3, but I want to give you a quick overview now.
This section will explore Rust’s function syntax, parameter passing, return values, and how Rust’s ownership model impacts functions.
In Rust, functions are defined using the fn keyword, followed by the function name, a list of parameters, and the function body enclosed in curly braces. Functions can take zero or more parameters and return a value.
fn main() {
println!("Hello, world!");
}
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn add(a: i32, b: i32) -> i32 {
a + b
}
Let’s break down the preceding example:
main is the entry point of a Rust...