Menu
Sign In Search Podcasts Libraries Charts People & Topics Add Podcast API Blog Pricing

Alice Ryhl

๐Ÿ‘ค Speaker
505 total appearances

Appearances Over Time

Podcast Appearances

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

This means that if you change the underlying code, now your test fails.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And so this means that you can't forget to update your examples in your documentation.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

I mean, no.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Yeah, you have to set a value before you use it for the first time.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

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.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

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.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

That's right.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So in Rust, it's not called switch.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

It's called match, but it's the same idea.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

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.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Of course, you can have a catch-all case if you want to.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

But most of the time, you would just list all the cases.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

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.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And I think this kind of leads to another way that Rust really helps with reliability, which is that if you're refactoring,

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

I think Rust is really good at telling you all the places you need to update.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

I've done this sometimes where I would refactor something.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

I change the code.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

I change the return type or whatever it is.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And then I just fix the compiler errors until the compiler stops shouting.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And then once I've done that, I've updated every place I need to update.