Search code examples
rustsyntactic-sugar

Assign a single value to multiple variables in one line in Rust?


A common way to assign multiple variables is often expressed in programming languages such as C or Python as:

a = b = c = value;

Is there an equivalent to this in Rust, or do you need to write it out?

a = value;
b = value;
c = value;

Apologies if this is obvious, but all my searches lead to Q&A regarding tuple assignment.


Solution

  • No, there is no equivalent. Yes, you have to write multiple assignments, or write a macro which itself does multiple assignments.