Skip to content

Commit

Permalink
functions and closures
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Nov 10, 2024
1 parent 4d13658 commit 7ce0959
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- [Essentials](./essentials/intro.md)
- [Core Concepts](./essentials/core-concepts/intro.md)
- [Functions](./essentials/core-concepts/function.md)
- [Closures](./essentials/core-concepts/closures.md)
- [Ownership](./essentials/core-concepts/ownership.md)
- [Borrow](./essentials/core-concepts/borrow.md)
- [Macros](./essentials/core-concepts/macro.md)
Expand Down
46 changes: 46 additions & 0 deletions src/essentials/core-concepts/closures.md
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.
7 changes: 6 additions & 1 deletion src/essentials/core-concepts/concurency.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Rust provides several ways to handle concurrency, ensuring safety and performanc

### 1. Threads

Rust's standard library provides a way to spawn threads using the `std::thread` module.
Rust's standard library provides a way to spawn threads using the `std::thread` module. The `thread::spawn` function is used to create a new thread. The || syntax is used to define a [closure](./closures.md), which is an anonymous function that can capture variables from its surrounding scope.

Here's a breakdown of the syntax:

`||`: This defines a closure with no parameters. If the closure had parameters, they would be listed between the vertical bars.
`{ ... }`: This is the body of the closure, which contains the code that will be executed in the new thread.

```rust
use std::thread;
Expand Down
77 changes: 77 additions & 0 deletions src/essentials/core-concepts/function.md
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.
7 changes: 5 additions & 2 deletions src/essentials/core-concepts/intro.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Core Concepts Specific to Rust
### Core Concepts in Rust

Rust has several core concepts that are fundamental to understanding and effectively using the language. These concepts are designed to ensure memory safety, concurrency, and performance without sacrificing ease of use. Here are some of the core concepts specific to Rust:

Expand Down Expand Up @@ -26,7 +26,7 @@ Traits are Rust's way of defining shared behavior. They are similar to interface

Enums in Rust are more powerful than in many other languages because each variant can have associated data. Enums are often used with pattern matching to handle different cases.

### 7. Concurrency
### 7. [Concurrency](./concurency.md)

Rust provides powerful concurrency primitives, such as threads and channels, while ensuring memory safety. The ownership and borrowing system helps prevent data races at compile time.

Expand All @@ -42,3 +42,6 @@ Macros in Rust provide a way to write code that writes other code (metaprogrammi

Modules and crates are Rust's way of organizing code. Modules allow you to group related code together, while crates are the unit of compilation and distribution.

### 11. [Functions](./function.md) and [Closures](./closures.md)

In Rust, functions are a fundamental building block for organizing and reusing code. Functions allow you to encapsulate logic, perform specific tasks, and return values.

0 comments on commit 7ce0959

Please sign in to comment.