Screwtape

Screwtape at

I'm working on a hobby Rust project with a bunch of plain-old-data records that somebody might plausibly want to store in a hashmap/dict or set, which means each item needs to be hashable, and the hash needs to be stable. In Python, making an item hashable is easy, but making the hash stable is hard, since Python doesn't really provide immutability, and the next-best-thing involves writing a lot of ugly getter functions. Meanwhile, Rust doesn't even understand immutability at all: if you own it and it has public fields, you can mess with them to your heart's content.

Turns out, Rust's got this covered: when you stick your data record into a hash-based collection, the collection takes ownership and will only hand out read-only references, so even if you own the collection you can't modify the items inside it. And I didn't even have to write a single getter function.

Charles Stanhope, AJ Jordan, Claes Wallin (韋嘉誠) likes this.