Skip to content

Commit a74bce3

Browse files
committed
functions and modules
1 parent a32c4f1 commit a74bce3

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

06. functions_and_modules.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
### 6. Functions and Modules: Building Modular and Reusable Code
2+
3+
#### Defining Functions in Rust
4+
5+
Functions in Rust are declared using the `fn` keyword followed by the function name, parameters, return type (optional), and function body. Here's a basic example:
6+
7+
```rust
8+
fn greet(name: &str) {
9+
println!("Hello, {}!", name);
10+
}
11+
```
12+
13+
- The `fn` keyword indicates the start of a function declaration.
14+
- Parameters are specified within parentheses after the function name.
15+
- The function body contains the code to be executed when the function is called.
16+
17+
#### Function Parameters and Return Values
18+
19+
Rust functions can have parameters and return values, allowing for flexible and reusable code. Parameters are passed by reference by default to prevent unnecessary copying of data. Here's an example:
20+
21+
```rust
22+
fn add(x: i32, y: i32) -> i32 {
23+
x + y
24+
}
25+
```
26+
27+
- Parameters are defined with their names and types within parentheses after the function name.
28+
- The `->` arrow syntax is used to specify the return type of the function.
29+
30+
#### Function Overloading and Polymorphism
31+
32+
Rust does not support traditional function overloading like some other languages. Instead, Rust achieves polymorphism through generics and traits. This allows for flexible and generic functions that can operate on different types. Here's a simple example using generics:
33+
34+
```rust
35+
fn print<T>(value: T) {
36+
println!("{}", value);
37+
}
38+
```
39+
40+
- The `<T>` syntax declares a generic type parameter.
41+
- The function can then accept arguments of any type that implements the `Display` trait.
42+
43+
#### Modules and the `mod` Keyword
44+
45+
Modules in Rust allow you to organize code into separate namespaces, making it easier to manage larger projects. You can create modules using the `mod` keyword followed by the module name and its contents. Here's an example:
46+
47+
```rust
48+
mod math {
49+
pub fn add(x: i32, y: i32) -> i32 {
50+
x + y
51+
}
52+
}
53+
```
54+
55+
- Modules are declared using the `mod` keyword followed by the module name.
56+
- The `pub` keyword is used to specify that the function is accessible from outside the module.
57+
58+
#### Organizing Code into Modules and File System
59+
60+
Rust follows a convention-based approach for organizing code into modules and files. Each Rust file corresponds to a module, and the directory structure reflects the module hierarchy. This allows for clear and intuitive code organization. Here's an example directory structure:
61+
62+
```
63+
src/
64+
├── main.rs
65+
└── math/
66+
├── mod.rs
67+
└── operations.rs
68+
```
69+
70+
#### Visibility and Access Control
71+
72+
Rust provides visibility modifiers (`pub` and `priv`) to control the visibility of functions, types, and modules. This ensures encapsulation and helps prevent unintended access to internal implementation details. Here's an example:
73+
74+
```rust
75+
mod math {
76+
pub fn add(x: i32, y: i32) -> i32 {
77+
x + y
78+
}
79+
}
80+
```
81+
82+
- The `pub` keyword makes the `add` function accessible from outside the module.
83+
84+
#### Best Practices
85+
86+
- Keep functions small and focused on a single task for better readability and maintainability.
87+
- Use parameters and return values to make functions reusable across different parts of your codebase.
88+
- Organize code into modules and files based on functionality to improve code organization and maintainability.
89+
- Use visibility modifiers (`pub` and `priv`) judiciously to control access to your code's internals.
90+
91+
#### Real-World Example
92+
93+
Imagine you're developing a library for mathematical operations in Rust. You use modules to organize functions related to addition, subtraction, multiplication, etc., and provide clear visibility controls to ensure the library's users only access the intended functionality.
94+
95+
#### Conclusion
96+
97+
Functions and modules are essential building blocks of Rust programming, enabling code reuse, organization, and encapsulation. By mastering these concepts, you'll be able to write modular, maintainable, and scalable Rust code with confidence.

0 commit comments

Comments
 (0)