# Structs: mutable versus immutable

**URL:** <https://discourse.julialang.org/t/structs-mutable-versus-immutable/53889>\
**Category:** New to Julia\
**Tags:** question, struct\
**Created:** [January 25, 2021, 7:02am UTC](https://discourse.julialang.org/t/structs-mutable-versus-immutable/53889 "2021-01-25T07:02:57Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![owinter92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owinter92/32/21346_2.png) [@owinter92](https://discourse.julialang.org/u/owinter92)\
**Post date:** [January 25, 2021, 1:19pm UTC](https://discourse.julialang.org/t/structs-mutable-versus-immutable/53889/21 "2021-01-25T13:19:40Z")

</div>

I just tried on some small part of different code and you are right. It was probably benchmark artifact.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [January 25, 2021, 9:49pm UTC](https://discourse.julialang.org/t/structs-mutable-versus-immutable/53889/22 "2021-01-25T21:49:09Z")

</div>

> [@lmiq](#):
>
> ```julia
> julia> function change_vec1!(vec)
> for s in vec
> s = TestStruct(2*s.a,s.b)
> end
> end
> change_vec1! (generic function with 1 method)
> 
> julia> function change_vec2!(vec)
> for s in vec
> @set s.a = 2*s.a
> end
> end
> change_vec2! (generic function with 1 method)
> 
> ```

Beware: both codes do absolutely nothing to the contents of the array.

Must be:

```julia
function change_vec1!(vec)
    for i in eachindex(vec)
        s = vec[i]
        vec[i] = TestStruct(2*s.a,s.b)
    end
end

function change_vec2!(vec)
    for i in eachindex(vec)
        s = vec[i]
        vec[i] = @set s.a = 2*s.a
    end
end

```

`x` inside `for x in itr` is a value of item, not a reference to it. Re-binding `x` inside the loop body does not change the value in `itr`. E.g.

```julia
julia> v = collect(1:5)
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> for x in v
           x += 5
           println(x)
       end
6
7
8
9
10

julia> v
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 25, 2021, 10:48pm UTC](https://discourse.julialang.org/t/structs-mutable-versus-immutable/53889/23 "2021-01-25T22:48:09Z")

</div>

> [@Vasily\_Pisarev](#):
>
> Beware: both codes do absolutely nothing to the contents of the array.

Yes… my bad. Then the benchmark above is wrong. Mutating the field of the mutable struct is faster than copying the immutable structs even if the array contained in the structure is small and static. Probably one can see a great performance benefit if all fields are immutable and we are mutating all of them (actually the threshold is well known, and the size up to which StaticArrays are recommended, ~10).

[Previous page](https://discourse.julialang.org/t/structs-mutable-versus-immutable/53889.md?page=1)
