Sebastian Scholz
๐ค SpeakerAppearances Over Time
Podcast Appearances
So instead of writing a huge state machine, basically, like that, you can write your code more freely, just at any given structure that you want, just passing around these types of keys.
Well, it's definitely used in the embedded world a lot, maybe not in the same kind of a way, but we've got the idea from crates like Embassy and actually various hardware abstraction layer crates that if you, in the beginning, they give you one struct that contains all of the peripherals, like a
But it's just serialized types for these peripherals.
And the idea behind that is that you can then, when you want to use one of these peripherals, you have to consume that type.
And that prevents you from using the same peripheral twice.
For example, pins.
If you think about microcontroller, it has pins that you can toggle on or off.
And if you decided at one point, yes, okay, I'm going to use this pin to, I don't know, to talk to a motor, for example.
And then later you think, okay, wait, I have another use for a pin.
Maybe, I don't know, I want to toggle an LED or something.
let me see, maybe pin one is still available.
I'm going to try to use it.
Ah, no, I've already used this before.
And Rust tells you this because you moved the type, you moved the variable into the function that used it.
And so you can't use it later.
And so that's kind of the same idea.
You have a token for each peripheral, for each pin that you can only use once.
And this is one of the things I really love about Rust is
It's the ability to detect these kind of configuration errors at compile time.
I mean, the compiler literally prevents you from writing code that is wrongly configured that would be valid code just from a processor perspective.