Rust is a modern systems programming language that provides support for defining and using functions and modules to organize code in a reusable manner.
In this document, we will discuss how to define and use functions in Rust, and how to organize your code into reusable modules.
Defining Functions in Rust
Functions are the building blocks of any program, and Rust provides a simple and concise syntax to define functions. In Rust, functions are defined using the fn
keyword followed by the function name, input parameters enclosed in parentheses, and the return type.
For example, let’s define a function that adds two numbers:
fn add_numbers(x: i32, y: i32) -> i32 {
x + y
}
In the above example, we have defined a function named add_numbers
that takes two parameters x
and y
of type i32
and returns their sum as an i32
.
Rust also supports defining functions with no return value using the ()
type. For example:
fn print_message(message: &str) {
println!("{}", message);
}
In the above example, we have defined a function named print_message
that takes a parameter message
of type &str
and prints it to the console.
Using Functions in Rust
Functions in Rust can be called by providing the input parameters. For example:
let result = add_numbers(10, 20);
In the above example, we are calling the add_numbers
function with input parameters 10
and 20
and storing the result in a variable named result
.
Organizing Code into Reusable Modules
As your Rust programs grow in size and complexity, it is important to organize your code into reusable units. This is where modules come in handy.
Modules can contain functions, structs, enums, constants, and other modules. To define a module, we use the mod
keyword followed by the module name and the curly braces enclosing the module content.
For example, let’s define a module math
that contains the add_numbers
function we defined earlier:
mod math {
pub fn add_numbers(x: i32, y: i32) -> i32 {
x + y
}
}
In the above example, we have defined a module named math
that contains a function named add_numbers
. The pub
keyword is used to make the function public and accessible from outside the module.
Modules can also be nested within other modules to create a hierarchy. For example:
mod math {
pub mod geometry {
pub fn calculate_area(radius: f64) -> f64 {
std::f64::consts::PI * radius * radius
}
}
}
In the above example, we have defined a nested module named geometry
within the math
module that contains a function named calculate_area
.
To use a function from a module, we need to use the use
keyword to bring the function into scope. For example:
use math::add_numbers;
let result = add_numbers(10, 20);
In the above example, we are using the add_numbers
function from the math
module by bringing it into scope using the use
keyword.
Conclusion
In this document, we have discussed how to define and use functions in Rust, and how to organize code into reusable modules. By using functions and modules, we can create reusable and maintainable code in Rust.
In summary, Rust’s support for functions and modules is one of its major strengths. Functions and modules allow you to write concise and maintainable code that can be easily reused across different parts of your programs.