My First Rust SO Accepted Answer
Pan Chasinga

Pan Chasinga @pancy

About: If Rumi were a programmer. I'm really good at self-proclaimed manifestos. Too reckless to be an entrepreneur. Open-source fanatic. Bare-metal hardware, virtual machine, and all-computing wisdom.

Joined:
May 14, 2017

My First Rust SO Accepted Answer

Publish Date: Jun 1 '20
7 4

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.

Joe Chasinga Newsletter BETA School

Comments 4 total

  • Andrei Gatej
    Andrei GatejJun 1, 2020

    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 🤔

    • Weekly Typed
      Weekly TypedJun 10, 2020

      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.

      • Andrei Gatej
        Andrei GatejJun 10, 2020

        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..

  • Weekly Typed
    Weekly TypedJun 10, 2020

    Fantastic work. Thanks for sharing here on DEV. May the reputation point be forever in your favor.

Add comment