Syntax int a,b,c in rust


int a,b,c;


let (a,b,c): (i32,i32,i32);???

How to do it in one assignment?

I'm slightly confused by what you mean, there are no assignments there. If you explicitly specify the type you can't avoid repeating it, but you don't have to.

let (a, b, c);

This is perfectly valid Rust as long as rustc is able to infer the types of a, b and c later on, e.g.

a = 5_i32;
b = 6 + 7;
c = 8;


It's worth noting that in Rust, by contrast with C,

  • you almost never declare a variable without initializing it at the same time (although this is possible as @Kestrer showed)
  • local type inference means that you don't usually have to annotate each variable declaration with a type: the compiler will figure out the right type based on how you use the variable later on (as Koxiaet also described)


1 Like

So, to sum up, you should probably write this as:

let (a, b, c) = (1, 2, 3);
1 Like

Or, if you wanted to initialize them all to the same value you can use array patterns, which scales a bit better for more bindings:

let [a, b, c] = [0_i32; 3];

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.