# Assigning struct to multiple variables

**URL:** https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763
**Category:** General Usage
**Tags:** destructuring
**Created:** [October 15, 2022, 2:33am UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763 "2022-10-15T02:33:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![HMegh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmegh/32/216684_2.png) [@HMegh](https://discourse.julialang.org/u/HMegh)
#### Post date: [October 15, 2022, 2:33am UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763/1 "2022-10-15T02:33:52Z")

</div>

I would like to export a struct to variables. Say I have the following struct

```julia
struct point
       x::Float64
       y::Float64
end

```

and I use it to define a point

```julia
A=point(3.0,4.0)

```

Is there a concise way to extract the components, like

```julia
x,y=A

```

However, this throws the following error

```julia
ERROR: MethodError: no method matching iterate(::point)

```

I am aware that `x,y=A.x, A.y` works. However, I am looking for little more concise since the structs that I work with contain many fields with long names.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [October 15, 2022, 2:52am UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763/2 "2022-10-15T02:52:38Z")

</div>

`(; x, y) = A`

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 15, 2022, 3:03am UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763/3 "2022-10-15T03:03:50Z")

</div>

I think you mean:

```julia
(; x,y) = A

```

See [property destructuring](https://docs.julialang.org/en/v1/manual/functions/#Property-destructuring) in the Julia manual.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [October 15, 2022, 3:26am UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763/4 "2022-10-15T03:26:33Z")

</div>

I edited my comment to fix it. The parenthesis are of course necessary, as otherwise it’s equivalent to ending the previous statement (the semicolon) and then normal iterative destructuring.

---

<div class="post-metadata">

### Author: ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)
#### Post date: [October 15, 2022, 3:39am UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763/5 "2022-10-15T03:39:21Z")

</div>

Unfortunately it doesn’t work in v1.6.x, but the manual doesn’t mention that.

---

<div class="post-metadata">

### Author: ![HMegh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmegh/32/216684_2.png) [@HMegh](https://discourse.julialang.org/u/HMegh)
#### Post date: [October 15, 2022, 4:38am UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763/7 "2022-10-15T04:38:10Z")

</div>

Works like a charm on v1.8, thanks!

---

<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: [October 15, 2022, 12:33pm UTC](https://discourse.julialang.org/t/assigning-struct-to-multiple-variables/88763/8 "2022-10-15T12:33:20Z")

</div>

In 1.6 you can use

```julia
using Parameters
@unpack x, y = p

```

Ps. If you are actually representing points, take a look at FieldVector type of Static arrays.jl
