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.
Top comments (4)
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 🤔
I feel you! I have been learning Rust for a few months now and I am still debating switching to Go for a little while.
I gotta say I LOVE Rust. But I also have to admit it can be a lot of effort sometimes. In the end, I feel like I could make progress more quickly learning Go, and then come back to Rust later.
I would hate to do it though. Rust is so good.
I’m 90% decided to try Go, as I tend toward web dev and I’m very curious to see how docker works. But, at the same time, I cannot see an article about rust and not to click on the “Save later” button..
Fantastic work. Thanks for sharing here on DEV. May the reputation point be forever in your favor.