Spot the bug in callback index capture

from Lambda expressions
Kotlin 2.4.10 intermediate 4 min 1 issue to find

Each callback should return the index from the iteration that created it. Find the bug.

kotlin
fun callbacks(): List<() -> Int> {
    val result = mutableListOf<() -> Int>()
    var index = 0
    while (index < 3) {
        result += { index }
        index++
    }
    return result
}

fun main() {
    println(callbacks().map { callback -> callback() })
}
Open in playground
Report an error