Why the Move variant doesn't have a bracket like other two variants?

enum Message {
    Move { x: i32, y: i32 },
    ChangeColor(i32, i32, i32),

What do you mean "why"? This is how the syntax of struct-like variants is defined.

Ok, then why other two variant have () bracket. Is it bcoz other two are tuple type?

Correct. From the Rust book:

enum Fieldless {

(enum variants all have the same type: that of the enum.)

1 Like

In Rust, both structs and enum variants come in three flavors

  • unit struct style - Foo
  • tuple struct style - Foo(i32, String)
  • ordinary struct style - Foo { n: i32, name: String}

the first (unit style) is way to make structs without fields particularly short and easy to work with, and for enum variants, this style most resembles enums from C

the second (tuple style) and third (ordinary style) are differentiated in whether you want the fields to be identified by their order (i. e. positional) or by their name (for ordinary structs and enum variants of this style, construction and pattern matching can put fields in any order you like).


That's not the book but the reference. The section in the book that first mentions tuple structs would be here and the page introducing enums also features examples of all styles.

I'm fact OP seems to be reading this very page from the book. They should probably just read further in the text, as it's all explained there

  • Move has named fields, like a struct does.

Defining an enum with variants such as the ones in Listing 6-2 is similar to defining different kinds of struct definitions, except the enum doesn’t use the struct keyword and all the variants are grouped together under the Message type.


Or maybe they skipped the previous chapter about structs, which might explain the confusion.


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.