3 ms·
> It turns out that the vast majority of Rust code is just fine using Arc and letting the reference counts handle your memory management. I wish there was more
by throwaway894345 6y ago
> It turns out that the vast majority of Rust code is just fine using Arc and letting the reference counts handle your memory management.
I wish there was more content in the Rust community that stressed this, or rather that made it clear what the happy path was so we don’t have to go off and learn it by trial and error. I always get the feeling that using Arc, etc gratuitously is a bad practice that may bite me later instead of “your code will be fine unless it really is perf sensitive (Steve’s sibling comment also kind of illustrates the lack of consensus about what is the happy path).
- steveklabnik 6y agoI think that folks are happy to say "oh just toss an Arc on it", and there's no like, moral opposition to doing so. But the issue is that it is much easier said than done. So for example, let's take a really simple program: fn takes_ref(r: &i32) { println!("r: {}", r); } fn main() { let v = 5; takes_ref(&v); } Sure. Not a big deal. But imagine that, for some reason, we are having issues here. We can toss an Arc on it, sure, no big deal: use std::sync::Arc; fn takes_ref(r: &i32) { println!("r: {}", r); } fn main() { let v = Arc::new(5); takes_ref(&v); } That's not too bad, we're only wrapping up the constructor. Okay, sure. But what happens when our requirements change, and we need to mutate something inside takes_ref? We can do that very easily in our original program: fn takes_ref(r: &mut i32) { *r += 1; println!("r: {}", r); } fn main() { let mut v = 5; takes_ref(&mut v); } But if we want to do this in our Arc world... we have to do this: use std::sync::{Arc, Mutex}; fn takes_ref(r: &mut i32) { *r += 1; println!("r: {}", r); } fn main() { let v = Arc::new(Mutex::new(5)); takes_ref(&mut v.lock().unwrap()); } This is way more boilerplate. And, I even messed it up the first time. So the end experience here is "ugh so much boilerplate in Rust", when often times, the answer is "don't use Arc/Rc Mutex/RefCell" They exist because, in some cases, you legitimately do need to use them. But if we encourage people to reach for them too early, they can have an even worse time.
- throwaway894345 6y agoThat makes sense, and this is exactly the kind of thing a newbie has to wade through without the benefit of knowing the tradeoffs in choosing one path or the other. There probably are docs that say when to use Arc and when not to, but there's still a cacophony of voices saying "just use Arc and deal with performance issues later" and it's hard for a newbie to understand whom to listen to. Note that this isn't a criticism of Rust, but rather a rough edge that I'm confident will eventually be polished off.
- steveklabnik 6y agoYep, absolutely agreed. I think we'll get there, just a lot of work to be done around figuring out how to turn new Rustaceans into intermediate ones without just saying "uhhh I dunno write some code and you'll get it."