STRUCT
how is it:
struct Color {
red: u8,
green: u8,
blue: u8
}
how would it be:
struct Color {
u8 red, green, blue,
}
TUPLE STRUCT
how is it:
struct Color(u8, u8, u8);
how would it be:
struct Color(u8; 3);
TUPLE
how is it:
let color: (u8, u8, u8) = (5, 9, 7);
how would it be:
let color: (u8; 3) = (5, 9, 7);
The syntax for creating a struct is already fairly concise. We don't need to shorten it to save a few characters at the expense of readability. If you really want to do this, then it should be fairly straightforward to create a macro to desugar from this to the normal struct declaration.
let int a= 8, b= 7, c= 9;
let int a := b := c = 4;
print!("{}, {}, {}", a, b, c); // outputs: 4, 4, 4
struct S {
u8 a, b, c; // ; instead , here the counterpoint
f64 d, e;
}
I don't see conflict or unreadability at first sight, but I understand the counterpoints
Part of me likes struct Color[u8; 3];, but the more realistic part of me says that struct Color([u8; 3]); is fine -- you can always deref it to the array if you want, or impl Index and friends on it.
let color: (u8; 3) = (5, 9, 7);
This one in particular the answer is that you should use an array instead of a tuple if you want this. let color: [u8; 3] = [5, 9, 7]; is fine. And now that array patterns are stable, let [r, g, b] = color; works too, so there's basically no need for tuples like this.
I'm going to close this because the ship has sailed for struct syntax. In particular, there's no way that rust will switch from $name: $type to $type $name -- for consistency it would need to also change in lets and function parameters, and the churn from that is so high that even if the latter were unambiguously better, Rust almost certainly still wouldn't change.
Part of me likes
struct Color[u8; 3];, but the more realistic part of me says thatstruct Color([u8; 3]);is fine -- you can always deref it to the array if you want, or implIndexand friends on it.let color: (u8; 3) = (5, 9, 7);
This one in particular the answer is that you should use an array instead of a tuple if you want this.
let color: [u8; 3] = [5, 9, 7];is fine. And now that array patterns are stable,let [r, g, b] = color;works too, so there's basically no need for tuples like this.I'm going to close this because the ship has sailed for struct syntax. In particular, there's no way that rust will switch from
$name: $typeto$type $name-- for consistency it would need to also change inlets and function parameters, and the churn from that is so high that even if the latter were unambiguously better, Rust almost certainly still wouldn't change.
I understood the difficulty with metaprogramming $type $name
but
let color: (u8; 3) = (5, 9, 7);
can not be an array, I cannot do
let color: (u8; 3, f64; 2) = (5, 9, 7, 4.9, 7.3);
with an array
@cindRoberta That's because tuples and arrays are very different types. If you feel the need for that syntax, write a macro.
Most helpful comment
The syntax for creating a struct is already fairly concise. We don't need to shorten it to save a few characters at the expense of readability. If you really want to do this, then it should be fairly straightforward to create a macro to desugar from this to the normal struct declaration.