Oh! Of course! Rust itself doesn't have `null`, but I can totally make more falsy Options, and that would enable me to have a lot more control about exceptions, like "missing," (API doesn't know it should be there) "empty," (API failed to give a value) or actually `false` (API knew it should be there and set it to `false`) being different, usable cases.

That should be better code than my go-to of negative INTs to communicate a specific error type.

Reply to this note

Please Login to reply.

Discussion

Yes, exactly! Rust lets you encode a lot more into your types than what you would normally be used to in other languages.

Regarding errors, if there’s semantic meaning attached to the negative ints in the API, you might want to have an Error type that captures that meaning on Rust side. Look into the thiserror crate that lets you easily spin up custom error types.

Yes. I started creating an online learning app for myself and encoded the negative to mean things like "never set," "user deleted," "missing data," etc. I don't think think I've used more (or lesser? 🤷‍♂️) than -3 in my code, but nothing prevents my using the whole range of an i8 in rust while also having robust exception handling.