Reputation: 25
So I have a function that returns a tuple of 2 values, and I want to assign these values to two different members of a struct. Is there a way to do this without having to call the function twice and extract each value individually?
I'm thinking something like:
let mut my_struct : MyStruct = MyStruct {
(member1, member2): function_that_returns_tuple()
}
Currently I am calling the function on two temporary variables and then moving them to the struct members but I'm wondering if there's a way to do it directly in the initialization.
Upvotes: 0
Views: 1235
Reputation: 127771
I believe that your existing approach is the correct one. If you name the variables as the struct members, you can avoid the explicit member: value
syntax:
let (member1, member2) = function_that_returns_tuple();
MyStruct { member1, member2, }
Upvotes: 1