-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d13658
commit 7ce0959
Showing
5 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
### Closures in Rust | ||
|
||
Closures are anonymous functions that can capture their environment. They are similar to lambdas in other programming languages. Closures in Rust are defined using the `|` syntax. | ||
|
||
### Basic Syntax | ||
|
||
```rust | ||
let add = |a, b| a + b; | ||
println!("Sum: {}", add(2, 3)); // Output: Sum: 5 | ||
``` | ||
|
||
### Capturing Environment | ||
|
||
Closures can capture variables from their enclosing scope. | ||
|
||
```rust | ||
let x = 10; | ||
let print_x = || println!("x: {}", x); | ||
print_x(); // Output: x: 10 | ||
``` | ||
|
||
### Mutable Captures | ||
|
||
Closures can also capture variables mutably. | ||
|
||
```rust | ||
let mut x = 10; | ||
{ | ||
let mut add_to_x = || x += 5; | ||
add_to_x(); | ||
} | ||
println!("x: {}", x); // Output: x: 15 | ||
``` | ||
|
||
### Moving Captures | ||
|
||
Closures can take ownership of captured variables using the `move` keyword. | ||
|
||
```rust | ||
let x = vec![1, 2, 3]; | ||
let print_x = move || println!("x: {:?}", x); | ||
print_x(); // Output: x: [1, 2, 3] | ||
// x is no longer accessible here | ||
``` | ||
|
||
Closures are a powerful feature in Rust, enabling concise and expressive code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
### Functions in Rust | ||
|
||
Functions are a fundamental building block in Rust. They allow you to encapsulate code into reusable blocks. Functions in Rust are defined using the `fn` keyword, followed by the function name, parameters, and the body of the function. | ||
|
||
### Defining a Function | ||
|
||
Here is a simple example of a function in Rust: | ||
|
||
```rust | ||
fn main() { | ||
println!("Hello, world!"); | ||
} | ||
``` | ||
|
||
In this example, `main` is a special function that serves as the entry point of a Rust program. | ||
|
||
### Function Parameters | ||
|
||
Functions can take parameters, which are specified in the parentheses after the function name. Here is an example: | ||
|
||
```rust | ||
fn greet(name: &str) { | ||
println!("Hello, {}!", name); | ||
} | ||
|
||
fn main() { | ||
greet("Alice"); | ||
greet("Bob"); | ||
} | ||
``` | ||
|
||
In this example, the `greet` function takes a single parameter `name` of type `&str`. | ||
|
||
### Return Values | ||
|
||
Functions can also return values. The return type is specified after an arrow (`->`). Here is an example: | ||
|
||
```rust | ||
fn add(a: i32, b: i32) -> i32 { | ||
a + b | ||
} | ||
|
||
fn main() { | ||
let sum = add(5, 3); | ||
println!("Sum: {}", sum); | ||
} | ||
``` | ||
|
||
In this example, the `add` function takes two parameters of type `i32` and returns their sum, which is also of type `i32`. | ||
|
||
### Early Returns | ||
|
||
You can return a value early from a function using the `return` keyword: | ||
|
||
```rust | ||
fn is_even(num: i32) -> bool { | ||
if num % 2 == 0 { | ||
return true; | ||
} | ||
false | ||
} | ||
|
||
fn main() { | ||
let number = 4; | ||
if is_even(number) { | ||
println!("{} is even", number); | ||
} else { | ||
println!("{} is odd", number); | ||
} | ||
} | ||
``` | ||
|
||
In this example, the `is_even` function returns `true` if the number is even and `false` otherwise. | ||
|
||
### Conclusion | ||
|
||
Functions in Rust are powerful tools for organizing and reusing code. They can take parameters, return values, and even return early if needed. Understanding how to define and use functions is essential for writing idiomatic Rust code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters