About Rust reference

I am reading the Rust reference and "Enumerated types" section contains the following sentence:
"Enum types cannot be denoted structurally as types, but must be denoted by named reference to an enum item" (whith emphasis on 'structurally').

Can anyone explain that in layman terms? How different is it from other "type of types" like struct types or... ?


For example, C++'s std::variant can only be denoted structurally as types, not by name. You can't even name each possible cases - only their type names.

It means that enums are sum types and not union types: you always construct a very specific variant by invoking its name (such as Some or Ok), and several variants can contain associated data of the same type. I.e., which variant an enum-typed value holds isn't magically inferred from the type of the associated data; it is always explicitly stated by the variant name, by whoever constructed it.

This means that you can have

enum Foo {

and you will be able to construct First(42) and Second(42), which are distinct and different values.

If enums were union types (not to be confused with untagged sum types declared by the union keyword in Rust), then the two u32s would be "squished together", and there would be no way to tell apart a Foo::First from a Foo::Second (and in fact Foo would be an alias for, or at least isomorphic to, u32, because the union of something with itself is just itself).

Thanks for the reply which makes sense but I still not fully understand the "cannot" part of the reference sentence.
Anyway, I don't think it's that important but it bothers me because it seems to be one example of this technical "jargon" I stumble upon a lot when reading PL references and make me think I will never fully grasp the true semantic of the language if I don't get those..

Cannot means there's no way to do it. You can't create an enum merely by specifying the wrapped type, you have to specify the variant name. There's simply no facility built into the language to infer the variant from the associated data.

I'm reading this sentence and cannot understand it (in particular the first half of the sentence) even with a CS degrees. In my opinion, it needs to be improved. Feel free to open an issue on GitHub - rust-lang/reference: The Rust Reference

Edit: I mean, my intuition tells me that it might meant that you cannot write something like enum {String, u32} (pseudo-syntax) to make up some kind of “anonymous” enum types on the fly, but you'll have to define an enum item that does what you want, and then name that item. But if that's what is meant, it's definitely not obvious, and for all I know, my interpretation might be totally off. Also, I don't know if I would've interpreted it this way without @Hyeonu's mention of C++'s std::variant.


Yeah, it's not well-phrased, and it contains words which are too general or somewhat ambiguous.

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.