Can I ignore some enum fields if I'm certain the variable is a specific enum?
Here's my code.
#[derive(Debug)]
struct Info {
start_time: u32
}
#[derive(Debug)]
enum State {
Running(Info),
Stopped
}
fn main() {
let info = Info { start_time: 333 };
let state = State::Running(info);
if let State::Running(state_info) = &state {
println!("{state_info:?}");
println!("{state:?}");
} else {
panic!("shouldn't be happen"); // This shouldn't happen..
}
}
enum State moves the struct Info after initialized. So if I want to work on Info afterward, I can't keep the reference to old info but must get it again.
However, in the above pattern, Rust compiler enforces to check all possible enums, so I should use if let. Can I achieve the same without unnecessary code? I wonder if I can do the same without if let / match / extra helper function / extra code depth / ...
No, your patterns have to be exhaustive at all times. If you find yourself doing this a lot, then your code is probably ill-structured and you should refactor it so that any variant-specific handling happens before wrapping the inner value in an enum.
if you feel the code is "unnecessary", chances are you have a design problem. as @H2CO3 suggested, you should consider refactor your code.
the closest thing you can do is to use let else with a unreachable!() assertion (or use the unsafe unreachable_unchecked hint if you know what you are doing).
rust enum is a sum type, and it's used for situations which the "shape" of the data can change at runtime. if you find yourself only need one particular "variant" at compile time, you probably want to use other constructs, like the typestate pattern.
Well, the actual code contains thread pool for handling incoming operations, so I only want to configure struct Info after I set state to State::Running. I'm not sure whether refactoring can help, but thank you for the confirmation.