Wow, the borrow checker in Rust is a sadist’s dream. I don’t have enough life to spend half of it trying to sweet talk a compiler into happiness. Giving up on the current exercise for now and may return to it once I’ve worked my way through the rest of the book. If anything, this experience is making me appreciate the sweet elegance of Swift even more now. ARC really hits the sweet spot of safety and ergonomics. (I know Rust has other priorities and that’s great but, heck, life’s too short.) :)
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 the trade-off is not between safety and ergonomics — Python's safe and ergonomic with its GC, but terribly slow. The trade-off is between ergonomics & performance and/or between safety & performance. You can say Swift and Vala's ARC is a sweet spot on both. Rust is for when you want even more performance. And it's not that hard, really, you'll get used to it.
@bugaevc Yeah, really loved Vala when I was playing with it about a month ago. Definitely one I want to did deeper into :)
@aral Its exactly why life is short, why the borrow checker exists. Most of the times it complains about things, is cause they are unsound and you would have only discovered them at runtime.
The compiler guarantees while they seem limiting at first, they really make working with large codebases much more efficient and greatly reduce the number of time spent hunting and fixing bugs.
Scripting people don't even understand why we need those * and &s in the first place, and Rust's appeal to them is performance and general low-level-ness
@aral Yeah, the rust "features" is what put me off for using it for anything sirious. I've personally found DLang, which i find to be very comfortable to code in.
@Clipsey Neat, thanks. Will put D on my list of languages to play around with for fun :)
@Clipsey Thank you :)
@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.