Skip to content

Commit

Permalink
flow control
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Nov 16, 2024
1 parent ab3aaca commit 49410ea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Essentials](./essentials/intro.md)
- [Core Concepts](./essentials/core-concepts/intro.md)
- [Functions](./essentials/core-concepts/function.md)
- [Flow Control](./essentials/core-concepts/flow-control.md)
- [Closures](./essentials/core-concepts/closures.md)
- [Ownership](./essentials/core-concepts/ownership.md)
- [Borrow](./essentials/core-concepts/borrow.md)
Expand Down
57 changes: 57 additions & 0 deletions src/essentials/core-concepts/flow-control.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
### Flow Control in Rust

Rust provides several options for flow control, including `if` statements, `while` loops, and more. Here are some simple examples for each:

### `if` Statements

The `if` statement allows you to execute code based on a condition.

```rust
let number = 5;

if number < 10 {
println!("The number is less than 10");
} else {
println!("The number is 10 or greater");
}
```

### `while` Loops

The `while` loop allows you to execute code repeatedly as long as a condition is true.

```rust
let mut count = 0;

while count < 5 {
println!("Count is: {}", count);
count += 1;
}
```

### `for` Loops

The `for` loop allows you to iterate over a range or collection.

```rust
for number in 1..5 {
println!("The number is: {}", number);
}
```

### `match` Statements

The `match` statement allows you to compare a value against a series of patterns and execute code based on which pattern matches.

```rust
let number = 3;

match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Something else"),
}
```

These are some of the basic flow control options available in Rust. Each of these constructs helps you manage the flow of your program effectively.
2 changes: 1 addition & 1 deletion src/essentials/core-concepts/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Borrowing allows you to reference a value without taking ownership of it.

Lifetimes are a way of ensuring that references are valid as long as they are used. They prevent dangling references and ensure memory safety. Lifetimes are often inferred by the compiler, but they can also be explicitly specified.

### 4. Flow Controls and Pattern Matching
### 4. [Flow Controls](./flow-control.md) and Pattern Matching

Pattern matching is a powerful feature in Rust that allows you to match complex data structures and execute code based on their shape. It is commonly used with `match` statements and `if let` expressions.

Expand Down

0 comments on commit 49410ea

Please sign in to comment.