Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum example is very confusing #828

Closed
tomjakubowski opened this issue Dec 8, 2016 · 4 comments
Closed

enum example is very confusing #828

tomjakubowski opened this issue Dec 8, 2016 · 4 comments
Labels

Comments

@tomjakubowski
Copy link
Contributor

http://rustbyexample.com/custom_types/enum.html

// Create an `enum` to classify someone. Note how both names
// and type information together specify the variant:
// `Engineer != Scientist` and `Height(i32) != Weight(i32)`. Each
// is different and independent.
enum Person {
    // An `enum` may either be `unit-like`,
    Engineer,
    Scientist,
    // like tuple structs,
    Height(i32),
    Weight(i32),
    // or like structures.
    Info { name: String, height: i32 }
}

This suggests that a person is either:

  • an engineer
  • a scientist
  • a height (wat)
  • a weight (wat)
  • an info (wat)

I understand that it's hard to cram examples of all the ways you can use an enum into a single example enum, but maybe it could be split into multiple example types instead? The current example is confusing and doesn't reflect the way Rust developers use enums in the real world.

@mdinger
Copy link
Contributor

mdinger commented Dec 9, 2016

The one in match is a little better. It used to be mainly about body shape but that was deemed offensive though it made a little more sense. You can do something like this:

enum Color {
    Red,
    Blue,
    Green
}

enum Doors {
    Two,
    Four,
    Other(i32)
}

enum Vehicle {
    Boat(Color),
    Car(Color, Doors),
    Plane { color: Color, doors: Doors}
}

Now the example is longer though. Is it much of a gain? The struct is always going to be a problem here because in this type of example, it is a stylistic choice chosen to differ from a tuple. Regardless, both advertise the main important characteristics of a enum: they can hold whatever you want in whatever order.

FWIW, I don't see much advantage in splitting struct enums out into their own example strictly to show that enums can hold structs too.

@Poddster
Copy link

This suggests that a person is either:

  • an engineer
  • a scientist
  • a height (wat)
  • a weight (wat)
  • an info (wat)

I can just about rationalise this if it's renamed to PersonProperties. i.e. A person can have the engineer job, and a height... and an info. (Ignoring the fact that info duplicates height).

Would a real rust user write it this way? No idea.

The example is match is a much better illustration.

@coder543
Copy link

I just came here to open a ticket on this. Every time I want to demonstrate Rust enums to someone, I open up this chapter of Rust By Example, and then I have to apologize profusely for how terrible this example is.

Maybe I'll open up a PR soon with a better example.

@steveklabnik
Copy link
Member

I would love to see this fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants