How to declare several mutable variables at the same time?

I can declare several constants as follows:

let (a, b, c) = (1, 0.0, 3); 

But why can't I do this with mutable variables?

let mut (a, b, c) = (1, 0.0, 3); throws a compilation error:

 error: expected identifier, found `(` --> <anon>:2:13 2 |> let mut (a, b, c) = (1, 0.0, 3); |> ^ 
+8
rust
source share
1 answer

The correct syntax

 let (mut a, mut b, mut c) = (1, 0.0, 3); 

Mutability is a binding property, and a , b and c are all different bindings, each of which is bound to a specific element of the tuple after matching the template. Thus, they can be individually changed.

If you want to specify a type, you can do this too:

 let (mut a, mut b, mut c): (u8, f32, i32) = (1, 0.0, 3); 

For numeric literals, you can also use the suffix form:

 let (mut a, mut b, mut c) = (1u8, 0.0f32, 3i32); 

Of course, there is no reason to do this for example code; it’s much easier to just have 3 separate operators.

declare several constants

These are not constants, they are just immutable variables. A const is a different concept.

+17
source share

All Articles