[Solved] Is any more simply way destructuring an Enum only hold struct tuple variants


#1
#[derive(Debug)]
enum Task {
    T1(usize),
    T2(usize),
    T3(usize),
    // ...
    // ...
}


fn main() {
    let t = Task::T1(10);

    // let Task::_(secs) = t;
    println!("delay {}, exec {:?}", secs, t)
    
}


#2

No, there isn’t. If there’s a field shared by all members of the enum, and you often need to access it, you should have this structure instead:

struct Task {
   num: TaskNumber,
   secs: usize
}

enum TaskNumber { T1, T2, T3, ... }

#3

thank you; I should not take this question for granted. even if enum variants is first-class types,T1(usize), T2(usize) would be different type;


#4

You can do something like this:

fn main() {
    use Task::*;
    let t = Task::T1(10);

    match t {
        T1(secs) | T2(secs) | T3(secs) =>
        println!("delay {}, exec {:?}", secs, t),
    }
}

#5

Yes, this should be the most simply way, thanks.