After years of on and off learning, I've finally earned my first Rust accepted answer on StackOverflow.
Try using an Enum type for the speech
field:
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum Speech {
Str(String),
StrArray(Vec<String>),
}
#[derive(Debug, Serialize, Deserialize)]
struct foo {
pub foo: String,
speech: Option<Speech>,
}
Enum is the go-to way to represent a variant type in Rust. See https://serde.rs/enum-representations.html for more detail.
I'm beginning to feel my increased power to fight the Borrow Checker. Like I'm a martial art student being able to evade a blow from the Sensei for the first time.
So if anyone is learning Rust, keep at it and don't get too stressed out climbing the learning curve. Just follow the instructions, and use lots of libraries to avoid facing the Borrow Checker head-on.
If you're interested in my non-programming thoughts, you can subscribe to my newsletter BETA School.
Congrats!!
Rust seems to be really interesting, I’m (almost) sure I’m going to try it one day. I’m currently on the fence over whether it should be Rust or Go 🤔