-
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
ab3aaca
commit 49410ea
Showing
3 changed files
with
59 additions
and
1 deletion.
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,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. |
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