| 1 | fn foo1(mut arr []int) { |
| 2 | for _, mut j in arr { |
| 3 | j *= 2 |
| 4 | } |
| 5 | } |
| 6 | |
| 7 | fn test_for_in_mut_val_of_array() { |
| 8 | mut arr := [1, 2, 3] |
| 9 | foo1(mut arr) |
| 10 | println(arr) |
| 11 | assert arr == [2, 4, 6] |
| 12 | } |
| 13 | |
| 14 | fn foo2(mut arr [3]int) { |
| 15 | for _, mut j in arr { |
| 16 | j *= 2 |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn test_for_in_mut_val_of_fixed_array() { |
| 21 | mut arr := [1, 2, 3]! |
| 22 | foo2(mut arr) |
| 23 | println(arr) |
| 24 | assert arr == [2, 4, 6]! |
| 25 | } |
| 26 | |
| 27 | fn foo3(mut m map[string][3]int) { |
| 28 | for i in 0 .. m['hello'].len { |
| 29 | m['hello'][i] *= 2 |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn test_fn_mut_val_of_map() { |
| 34 | mut m := { |
| 35 | 'hello': [1, 2, 3]! |
| 36 | } |
| 37 | foo3(mut m) |
| 38 | println(m) |
| 39 | assert '${m}' == "{'hello': [2, 4, 6]}" |
| 40 | } |
| 41 | |
| 42 | fn foo4(mut m map[string][3]int) { |
| 43 | for _, mut j in m['hello'] { |
| 44 | j *= 2 |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn test_for_in_mut_val_of_map() { |
| 49 | mut m := { |
| 50 | 'hello': [1, 2, 3]! |
| 51 | } |
| 52 | foo4(mut m) |
| 53 | println(m) |
| 54 | assert '${m}' == "{'hello': [2, 4, 6]}" |
| 55 | } |
| 56 | |
| 57 | fn test_for_in_mut_val_of_map_direct() { |
| 58 | mut m := { |
| 59 | 'foo': 1 |
| 60 | 'bar': 2 |
| 61 | } |
| 62 | for _, mut j in m { |
| 63 | j = 3 |
| 64 | } |
| 65 | println(m) |
| 66 | assert '${m}' == "{'foo': 3, 'bar': 3}" |
| 67 | } |
| 68 | |
| 69 | fn test_for_in_mut_val_of_map_fixed_array() { |
| 70 | mut m := { |
| 71 | 'foo': [{ |
| 72 | 'a': 1 |
| 73 | }]! |
| 74 | 'bar': [{ |
| 75 | 'b': 2 |
| 76 | }]! |
| 77 | } |
| 78 | for _, mut j in m { |
| 79 | j = [{ |
| 80 | 'c': 3 |
| 81 | }]! |
| 82 | } |
| 83 | println(m) |
| 84 | assert '${m}' == "{'foo': [{'c': 3}], 'bar': [{'c': 3}]}" |
| 85 | } |
| 86 | |
| 87 | fn test_for_in_mut_val_of_string() { |
| 88 | b := 'c' |
| 89 | mut c := ['a', 'b'] |
| 90 | mut ret := []string{} |
| 91 | for mut a in c { |
| 92 | a = a + b |
| 93 | ret << a |
| 94 | } |
| 95 | println(ret) |
| 96 | assert ret == ['ac', 'bc'] |
| 97 | } |
| 98 | |
| 99 | fn test_for_in_mut_val_of_float() { |
| 100 | mut values := [1.0, 2, 3] |
| 101 | println(values) |
| 102 | |
| 103 | for mut v in values { |
| 104 | v = 1.0 |
| 105 | v = v + 1.0 |
| 106 | } |
| 107 | println(values) |
| 108 | assert values == [2.0, 2, 2] |
| 109 | } |
| 110 | |