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.

@alatiera @aral

> greatly reduce the number of time spent hunting and fixing bugs.

And writing tests.

Whole class of unit tests suddenly become completely unnecessary when the compiler guarantees things like types and access.

@alatiera @aral no no no, "it would be unsound in C but gcc wouldn't complain and you'd get bugs later" is how you sell Rust to C hackers. @aral is coming from a Swift & JS background, so that is not going to resonate with hos experience.

@alatiera @aral C hackers already understand ownership/borrowing/lifetimes (but are not used to the compiler enforcing them), and Rust's appeal to them is safety

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

@bugaevc @alatiera Oh no, wait, I lie, I did touch it in 1993 to create the back-end of a Web app but moved to PHP the moment it came out :)

@alatiera @aral good explained…

Also heard that it is hard often, but it is worth it… It really is worth it @aral … 😜

@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 :)

@aral You don't have to clone it. Do `for (department_name, employees) in departments.iter()` instead.

io::stdout().flush().expect("Die in a fire! Love, Rust."); 😂😂

@aral so it is actually really simple. Playground link: patch:

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.

(cc @schlink)

@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

@bugaevc @schlink Neat, thank you; that does look & feel better :) Any special reason (apart from brevity) that you used the vec! macro instead of the factory method on Vec?

@aral @schlink not really, both are fine. I think I find vec![] slightly more idiomatic, but only slightly & I'm not sure others do.

@bugaevc Gotta vent the frustration somewhere amirite? ;P

@aral D is a mature systems language which can be as safe as Rust with a lot less hassle. I like that the code you write looks like Python, but after compilation runs like C++.

Sign in to participate in the conversation
Aral’s Mastodon

This is my personal Mastodon.