Noob question about (data) struct(ure) syntax


Stupid syntax question: Why does assigning a value of a struct component happen via a colon? This confuses something, since the colon is actually typical for the data type specification?
And why is the name of the structure (generally a data structure) not written like a data type after a colon, but after the assignment operator (=)?

Sorry for that stupid question, but I’m a bit screwed up by C.

struct Point
    x: i32,
    y: i32,

// why not: 
 let origin: Point = {x = 0, y = 0};

// instead of:
 let origin = Point {x: 0, y: 0};  // origin: Point

mh ok… first looks a little bit unaesthetic with multiple „=“ :smiley:


Well, part of the reason that it’s not that is because it’s ambiguous on seeing the { whether it’s going to be a block (let origin: Point = { x }; is legal syntax) or a struct literal (struct Point { x } is legal syntax).

I don’t know why it’s : instead of =, though.


Once upon a time the core team discussed changing struct literals to use =, and various arguments were made in favor of the change, but in the end, inertia won out. (It would have required updating basically every Rust file in existence, at a time when the team was trying to stabilize the language and get 1.0 out the door.)

The current syntax with : is shared with languages like JavaScript, Go, and Python (and now Swift).

The Rust compiler was originally written in OCaml, which uses = in struct literals, so Graydon was clearly aware of that syntax but chose : anyways, for whatever reasons.


Thank you for the explanation and the referenced discussion. Obviously, this question does not seem to be so wrong.
The explanations concerning the „=“ make sense.

kballard commented on 6 May 2014 :

I disagree with this proposal. Variable assignment is, well, assignment. But fields in struct initializers are declarations of value, not assignments of value. I also agree with @pcwalton about type-syntax-follows-initialization-syntax.

tbu- commented on 12 May 2014:

As you can see I parse the : as is. It isn’t really an assignment because you’re referencing an instantiation of the struct.

let x = SomeStruct { x: 4 };
      ^ assignment happens here
let x = SomeStruct { x: 4 };
                      ^ this is just describing one possible instance of SomeStruct

Another syntax question, which comes to mind here, concerns the brackets of data structures. Tuple structs look like a function call:

struct Color(i32, i32, i32);

let black = Color(0, 0, 0);

Is that on purpose? Because I find that somehow obvious, alternatively with square brackets to highlight the difference to a common tuple or to blocks of statement:

struct Color [i32, i32, i32];

let black: Color = [0, 0, 0];

or generally for structures:

struct zhl [a: i32, b: f64, …]
let beispiel: zhl = [b: 6597.87, a: 76, …];


About your suggestion:

let black: Color = [0, 0, 0];

This syntax suggests that the struct name can be omitted! However this information is important for the compiler, so I think the struct name should stand directly in front of the values:

let black = Color [0, 0, 0];

Apart from that, in my opinion the square brackets are too similar to an array or to slice syntax (if the tuple struct contains 2 values).

About this code:

let black = Color(0, 0, 0);

Yes, someone could confuse this with a function, but I don’t consider this a problem. If it was a function, it would probably return a Color according to the function name, so it doesn’t make a difference whether it is a tuple struct or a function.


An RFC even describes that they “can be thought of” in that way:

(It’s not exactly the same, though, because of patterns. But in expressions it really is just like a function.)


your elucidations are very plausible and helpful for a better understanding of the language

thanks for answering :slight_smile:

It’s nice how well Rust is thought out,
as well as the language is discussed down to the smallest detail


You can even use the tuple struct constructor where a closure is expected.