Geom3trik
Geom3trik

Reputation: 25

Is it possible to define tuples as members of structs in Rust?

I am very new to Rust and I was wondering if it's possible to define a tuple as a struct member. Something like:

struct MyStruct {
    (x, y) : (u32, f32)
}

The compiler complains about the first comma, so this obviously isn't the right syntax. Is it even possible? I Can't find anything in the documentation, and if I search for tuple and struct I get results for tuple structs which is not what I'm looking for.

For anyone interested why I want to know this, I have a function that returns a tuple and I want to store the result inside a member of a struct. Currently I am calling the function on two temporary variables and then moving the results into two different struct members, but not sure if this is the right way to do it.

Upvotes: 0

Views: 1488

Answers (1)

Lukazoid
Lukazoid

Reputation: 19416

A tuple is a single variable which contains 2 values so when you define it in your struct it is still a single variable/field:

struct MyStruct {
    x: (u32, f32),
}

Upvotes: 2

Related Questions