Global Feed Post Login
Replying to Avatar Rusty Russell

Since I'm taking a few weeks vacation, I've decided to seriously try to learn rust. My method in this case is to ask ChatGPT to guide me (but not write for me!) a library ("crate") that I've always wanted to write and never got around to.

Of course, I get a lot of feedback on appropriate rust styling, but some of it veers into things I feel are deeper constraints. In this case, I had an open function, which took a struct containing some flags, such as "writeable", "create if didn't exist".

It didn't like the fact that I asserted if you set create and didn't set writeable. Here is my response:

---

Re: assert!(). I dislike APIs which allow misuse. Callers of a library should not rely on such checks in the library, in fact the concept of adding an InvalidOptions error type is offensive. A recoverable error is strictly a worse api than an unignorable error. But a compile time error is better.

We should use an enum READONLY, WRITE_MUST_EXIST, WRITE_MAY_CREATE.

---

Of course, it's a waste of time for me to lecture it on style, but I can't help myself!!

Avatar
Matt Corallo 2w ago 💬 1

Honestly preferring an enum and avoiding the assert is incredibly Rust-y.

Reply to this note

Please Login to reply.

Discussion

Avatar
Rusty Russell 2w ago

Leaning into the type system is one of the distinctive traits of my C code, too (and something which I really miss in Python, with its tacked-on type annotations). But Rust certainly lends itself well to this style!

Thread collapsed