Skip to content

Commit

Permalink
Merge pull request #197 from erjohnson/for-loop-edits
Browse files Browse the repository at this point in the history
Edit for loops section, add some examples
  • Loading branch information
Skytrias authored Jun 23, 2024
2 parents 0ae6c06 + 3fe1555 commit 671e3a4
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions content/docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,29 @@ where `a..=b` denotes a closed interval `[a,b]`, i.e. the upper limit is *inclus

Certain built-in types can be iterated over:
```odin
some_string := "Hello, 世界"
for character in some_string {
fmt.println(character)
}
some_array := [3]int{1, 4, 9}
for value in some_array {
fmt.println(value)
}
some_slice := []int{1, 4, 9}
for value in some_slice {
fmt.println(value)
}
some_dynamic_array := [dynamic]int{1, 4, 9}
defer delete(some_dynamic_array)
for value in some_dynamic_array {
fmt.println(value)
}
some_map := map[string]int{"A" = 1, "C" = 9, "B" = 4}
defer delete(some_map)
for key in some_map {
fmt.println(key)
}
Expand All @@ -329,9 +340,17 @@ for key, value in some_map {
```
The iterated values are *copies* and cannot be written to.

**Note:** When iterating across a string, the characters will be `rune`s and not bytes. `for in` assumes the string is encoded as UTF-8.
When iterating a string, the characters will be `rune`s and not bytes. `for in` assumes the string is encoded as UTF-8.

```odin
str: string = "Some text"
for character in str {
assert(type_of(character) == rune)
fmt.println(character)
}
```

It is also possible to iterate over arrays and slices in a by-reference manner by prepending a `&` to the value:
You can iterate arrays and slices by-reference with the [address operator](#address-operator):
```odin
for &value in some_array {
value = something
Expand All @@ -348,20 +367,27 @@ for &value, index in some_dynamic_array {
}
```

Maps can have their *values* iterated in a by-reference manner but not their keys which are immutable:
Map values can be iterated by-reference, but their keys cannot since map keys are immutable:

```odin
some_map := map[string]int{"A" = 1, "C" = 9, "B" = 4}
defer delete(some_map)
for key, &value in some_map {
value = something
value += 1
}
fmt.println(some_map["A"]) // 2
fmt.println(some_map["C"]) // 10
fmt.println(some_map["B"]) // 5
```


**Note:** It is not possible to iterate a string in a by-reference manner as strings are immutable.

#### `for` reverse iteration

Recently a special directive was added which allows to `#reverse` the above mentioned range based iteration.
The `#reverse` directive makes a range-based `for` loop iterate in reverse.

```odin
array := [?]int { 10, 20, 30, 40, 50 }
Expand Down

0 comments on commit 671e3a4

Please sign in to comment.