So given that I can’t let things go… went back to the exercise and finished it by overly simplifying it to meet the stated goals:
Still, it doesn’t feel right. Why should I have to clone the departments hashmap in the "link" arm of the match statement, for example (apart from keeping the compiler happy).
You can also the see rabbit whole I went into in the linked commit from the readme.
As always, thoughts/comments/improvements welcome :)
@aral You don't have to clone it. Do `for (department_name, employees) in departments.iter()` instead.
@fabricedesre Thanks, Fabrice :)
io::stdout().flush().expect("Die in a fire! Love, Rust."); 😂😂
@aral so it is actually really simple. Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=e628acc15ea0f4761f85094314a4a716 patch: https://paste.gnome.org/piqd3mfds
When iterating over a collection you don't want the loop to consume, make sure to *borrow*, not move, it. And I've made the "add" arm a tad nicer using if let & the entry API.
@aral @schlink the reason why your original approach w/ structs failed is because Rust *really* doesn't want you to represent your data as a "web" of objects that all reference each other. That works somewhat well with GC, not quite well but okay-ish with ARC and does not work at all with explicit memory management.
Instead, Rust wants you to get your *ownership* story clear. What owns what? I'd say `Company` keeps a list of its departments and a `Department` keeps a list of its employees.
@aral @schlink you see, learning Rust is not just about memorizing the syntax, you really need to change the way you think about some things. And not just how you think when writing Rust, it kind of opens a new perspective on how you design your code's architecture... and stuff.
It's hard to describe, really... I've already tried to once, back when I used to use Quora, perhaps read that instead of the last paragraph, lol https://www.quora.com/Is-Rust-worth-learning/answers/24194034
@bugaevc Gotta vent the frustration somewhere amirite? ;P
This is my personal Mastodon.