I just started learning Rust and noticed that the following code works, even though the various options inside the enum seem to get propagated to outside of the enum, thus being in the same namespace as the structs:
enum FooBar { Foo(int), Bar(uint) }
struct Foo { foo: int }
struct Bar { bar: uint }
This however causes a compiler error:
enum FooBar { Foo(int), Bar(uint) }
enum BarBaz { Bar(uint), Baz(f32) }
It almost seems as if structs and enums live in their own parallel universes. I would appreciate an explanation about how struct and enum typenames work in Rust and why this effect arises.
Also, can an already existing type (i.e. a struct) be one of the options inside the enum?
Foo
,Bar
,Baz
are constructors. Rust's structs are records, withfoo
andbar
as field names. They belong to different categories of names (or namespaces). – user824425