Alice Ryhl
๐ค SpeakerAppearances Over Time
Podcast Appearances
This means that if you change the underlying code, now your test fails.
And so this means that you can't forget to update your examples in your documentation.
I mean, no.
Yeah, you have to set a value before you use it for the first time.
Yeah, so there's a pretty cool library called Cerde in Rust where you have your struct, so your type with fields, and then you can say, I want to be able to parse this from, for example, JSON.
And then Cerde, it's a macro, it will generate code which checks the JSON that it's in the same format, it has all the fields you need, and they have the right types, and it generates native code that's specific to that particular shape of JSON, so it's also really efficient.
That's right.
So in Rust, it's not called switch.
It's called match, but it's the same idea.
You can match on your enum, and then you can have a branch for each possibility around B. And if you are missing one, that's a compiler error.
Of course, you can have a catch-all case if you want to.
But most of the time, you would just list all the cases.
And then in the future, when you add a new variant, the compiler will tell you, oh, you need to update your code here, here, here, and here.
And I think this kind of leads to another way that Rust really helps with reliability, which is that if you're refactoring,
I think Rust is really good at telling you all the places you need to update.
I've done this sometimes where I would refactor something.
I change the code.
I change the return type or whatever it is.
And then I just fix the compiler errors until the compiler stops shouting.
And then once I've done that, I've updated every place I need to update.