Skip to main content
deleted 3 characters in body
Source Link
Peter Hall
  • 59.9k
  • 17
  • 170
  • 231

The variants of an enum are values and all have the same type - the enum itself. A function's arguments are variablesfunction argument is a variable of a given type, and the function body must be valid for any value of that type. So what you want to do will just not work.

However, there is a common pattern for designing enums, which might help here. That is, to use a separate struct to hold the data for each enum variant. For example:

enum Group {
    OfTwo(OfTwo),
    OfThree(OfThree),
}

struct OfTwo { first: usize, second: usize }
struct OfThree { one: usize, two: usize, three: usize }

fn proceed_pair(pair: OfTwo) {

}

Anywhere that you previously matched on the enum like this:

match group {
    Group::OfTwo { first, second } => {}
    Group::OfThree { first, second, third } => {}
}

You would replace with:

match group {
    Group::OfTwo(OfTwo { first, second }) => {}
    Group::OfThree(OfThree { first, second, third }) => {}
}

The variants of an enum are values and all have the same type - the enum itself. A function's arguments are variables of a given type, and the function body must be valid for any value of that type. So what you want to do will just not work.

However, there is a common pattern for designing enums, which might help here. That is, to use a separate struct to hold the data for each enum variant. For example:

enum Group {
    OfTwo(OfTwo),
    OfThree(OfThree),
}

struct OfTwo { first: usize, second: usize }
struct OfThree { one: usize, two: usize, three: usize }

fn proceed_pair(pair: OfTwo) {

}

Anywhere that you previously matched on the enum like this:

match group {
    Group::OfTwo { first, second } => {}
    Group::OfThree { first, second, third } => {}
}

You would replace with:

match group {
    Group::OfTwo(OfTwo { first, second }) => {}
    Group::OfThree(OfThree { first, second, third }) => {}
}

The variants of an enum are values and all have the same type - the enum itself. A function argument is a variable of a given type, and the function body must be valid for any value of that type. So what you want to do will just not work.

However, there is a common pattern for designing enums, which might help here. That is, to use a separate struct to hold the data for each enum variant. For example:

enum Group {
    OfTwo(OfTwo),
    OfThree(OfThree),
}

struct OfTwo { first: usize, second: usize }
struct OfThree { one: usize, two: usize, three: usize }

fn proceed_pair(pair: OfTwo) {

}

Anywhere that you previously matched on the enum like this:

match group {
    Group::OfTwo { first, second } => {}
    Group::OfThree { first, second, third } => {}
}

You would replace with:

match group {
    Group::OfTwo(OfTwo { first, second }) => {}
    Group::OfThree(OfThree { first, second, third }) => {}
}
Source Link
Peter Hall
  • 59.9k
  • 17
  • 170
  • 231

The variants of an enum are values and all have the same type - the enum itself. A function's arguments are variables of a given type, and the function body must be valid for any value of that type. So what you want to do will just not work.

However, there is a common pattern for designing enums, which might help here. That is, to use a separate struct to hold the data for each enum variant. For example:

enum Group {
    OfTwo(OfTwo),
    OfThree(OfThree),
}

struct OfTwo { first: usize, second: usize }
struct OfThree { one: usize, two: usize, three: usize }

fn proceed_pair(pair: OfTwo) {

}

Anywhere that you previously matched on the enum like this:

match group {
    Group::OfTwo { first, second } => {}
    Group::OfThree { first, second, third } => {}
}

You would replace with:

match group {
    Group::OfTwo(OfTwo { first, second }) => {}
    Group::OfThree(OfThree { first, second, third }) => {}
}