mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Compound assign
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// compound assignment operators: +=, -=, *=, /=
|
||||
|
||||
fn main() {
|
||||
let x = 10
|
||||
println(x)
|
||||
|
||||
x += 5
|
||||
println(x) // 15
|
||||
|
||||
x -= 3
|
||||
println(x) // 12
|
||||
|
||||
x *= 2
|
||||
println(x) // 24
|
||||
|
||||
x /= 4
|
||||
println(x) // 6
|
||||
|
||||
// compound assignment on arrays (field access for structs/maps)
|
||||
let m = { "count": 0 }
|
||||
m["count"] += 1
|
||||
println(m["count"]) // 1
|
||||
|
||||
m["count"] += 10
|
||||
println(m["count"]) // 11
|
||||
|
||||
// compound assignment on array index
|
||||
let arr = [10, 20, 30]
|
||||
arr[1] += 5
|
||||
println(arr[1]) // 25
|
||||
|
||||
arr[2] *= 3
|
||||
println(arr[2]) // 90
|
||||
}
|
||||
Reference in New Issue
Block a user