Syntax int a,b,c in rust

C:

int a,b,c;

Rust:

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;
2 Likes

Thanks.

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)
3 Likes

Thanks.

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];
6 Likes