It was just a trivial example.
Yeah, I didn’t consider panicking in my example, so I assumed it would be fine to leave an invalid state temporarily, as long as I returned it to a valid state before leaving the function. (Like how
std::mem::replace has invalid state during the function after the value is copied out and before the replacement is copied in, but obviously the lack of a closure containing arbitrary code makes this safe.)
For some reason this bothers me. I think it’s the fact that although it is now guaranteed to be a valid state as far as Rust is concerned, if the code panics while the value is being manipulated, the program will still be in an invalid state because the value being manipulated should never be empty outside of the function. (If you argue that the code being run will never panic, then there shouldn’t be any problem with the initial solution using
std::mem::uninitialized [correct me if I’m wrong].)
What if the implementation of the enum is not defined by me?
Am I wrong in saying that if I never use the value, it cannot cause any unintended consequences? Since the value is just a placeholder while the actual value is being manipulated, I don’t ever actually use the