TLDR; How do you make Rust's pattern matching behave dynamically like Python 3?
I'm quite new to Rust, and I struggle with a certain concept / code snippet. I'm trying to assign an object that is unknown at compile time to a variable using pattern matching. The different objects share a common trait, which contains the only methods I want to use on this object.
trait DoAction { fn action(&self); }
struct ObjectA { ... }
impl DoAction for ObjectA { fn action(&self) { ... } }
struct ObjectB { ... }
impl DoAction for ObjectB { fn action(&self) { ... } }
// This does not work
let object = match condition {
true => ObjectA { ... },
false => ObjectB { ... },
};
// I want to call this
object.action();
Working Python 3 example for comparison (what I want to achieve).
object = ObjectA(...) if true else ObjectB(...)
All help on this would be appreciated!