# What is the difference between x .= and x\[:\] =

**URL:** <https://discourse.julialang.org/t/what-is-the-difference-between-x-and-x/64790>\
**Category:** General Usage\
**Created:** [July 17, 2021, 2:10am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-x-and-x/64790 "2021-07-17T02:10:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 17, 2021, 2:10am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-x-and-x/64790/1 "2021-07-17T02:10:34Z")

</div>

```julia
julia> x = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

```

In this case they are the same. Are they always the same? What is the difference?

```julia
julia> x .= [10, 20, 30]
3-element Vector{Int64}:
 10
 20
 30

julia> x[:] = [11, 21, 31]
3-element Vector{Int64}:
 11
 21
 31

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 17, 2021, 4:20am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-x-and-x/64790/2 "2021-07-17T04:20:23Z")

</div>

I am not sure the second form (namely `x[:] = ...`) works with [broadcast fusion](https://docs.julialang.org/en/v1/manual/performance-tips/#More-dots:-Fuse-vectorized-operations).

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [July 17, 2021, 8:03am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-x-and-x/64790/3 "2021-07-17T08:03:19Z")

</div>

One difference is that `x[:] = ...` treats `x` as a vector, while `x .= ...` uses the actual shape of `x`:

```julia
julia> x = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> x .= [10, 20, 30, 40]
ERROR: DimensionMismatch("array could not be broadcast to match destination")
[...]

julia> x[:] = [10, 20, 30, 40]
4-element Vector{Int64}:
 10
 20
 30
 40

julia> x
2×2 Matrix{Int64}:
 10 30
 20 40

```
