This commit is contained in:
JMARyA 2024-02-12 08:30:27 +01:00
parent 68cc0a4a34
commit b911a95873
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 30 additions and 6 deletions

View file

@ -107,6 +107,31 @@ while condition {
}
```
You can break out of a loop or continue to the next iteration:
```rust
for i in 0..10 {
if i % 2 == 0 {
continue
}
if i % 5 == 0 {
break;
}
println!("{i}");
}
```
If you have nested loops you can label them and use `break` and `continue` on specific loops:
```rust
'outer_loop: for i in 0..10 {
'inner_loop: for x in 0..10 {
if (x*i) > 40 {
continue 'outer_loop;
}
print("{x} {i}");
}
}
```
### Functions
One can use functions with optional arguments and return types. If you `return` on the last line, you can ommit the `return` keyword and `;` to return the value.
```rust