6 ms·
> In fairness, "just use Rc<>/Arc<>/clone()/etc" is common advice from the Rust community in response to criticism that the borrow checker puts an undue burden
by steveklabnik 5y ago
> In fairness, "just use Rc<>/Arc<>/clone()/etc" is common advice from the Rust community in response to criticism that the borrow checker puts an undue burden on code paths which aren't performance sensitive.
Yes, and I think it's good to push back on that. I personally feel it's pretty misguided. I've been meaning to write about this, but haven't... so I'll just leave a comment, heh. Someday...
A related comment I wrote a while back about this: https://news.ycombinator.com/item?id=24992747 https://news.ycombinator.com/item?id=24992747
- throwaway894345 5y agoIf all you do is publish that link, it would not be a disservice to your readers IMHO.
- oconnor663 5y ago"Just use clone" seems like reasonable advice to give people on day 1 or week 1. I guess the hazard there is that they might end up writing `fn foo(s: String)` everywhere instead of `fn foo(s: &str)`, but they can gradually learn the better thing through case-by-case feedback, and correcting this is usually a small change at each callsite rather than a total program rewrite. On the other hand "just use Arc" is definitely smelly advice. I think going down that route, a new Rust programmer is likely to try to do something that really won't ever compile, and wind up really frustrated. Maybe we can distinguish this often-really-bad advice from the other mostly-ok-at-first advice?
- timClicks 5y agoAgree. Starting with .clone() is very useful for beginners, in my opinion. It gives people a chance to learn what ownership is gradually.
- steveklabnik 5y ago"Just use clone" is absolutely fine. I even put it in the book! Yes, I guess to me they're distinct, but I can totally see how it may seem similar. I'll try to make sure to make that explicit when I talk about this, thanks!
- bsder 5y agoThanks you for that example. You really should put that somewhere public. However, that example commits the Rust "sin" I always see with "spawn" explanations in that it packs everything into a closure. Please, please, please ... for the sake of newbies everywhere ... please define a function and call that function from inside the spawn closure. This is one of the Rust things I spend the most time explaining and unpacking for junior engineers. The separate function makes explicit the variables that are moving/borrowing/cloning around, which names are external/passed/closure-specific, and how it all ties together. It breaks apart the machinery that gets all conflated with the closure. They can always make it succinct again later once they understand the machinery more completely. This gets particularly bad with "event loops" (I have a loooooong rant I need to write up about event loops and why they are evil--and it's not Rust specific) and deep "builder chains" (which I consider a Rust misfeature that desperately needs to go away).
- steveklabnik 5y agoThat is a very interesting idea that I find viscerally unsettling but will consider, thanks. It is probably because of all my years with Ruby that this seems so off to me, but also, I hear you. Hmm.
- bsder 5y agoI get why its unsetlling. I hear you, too. The issue in Rust is that "spawn" is generally the first time that a programmer is forced to confront closures--before that they can completely ignore them. It is a quirk of Rust that someone who "just wants to spawn a thread" suddenly gets all this language overhead dumped on them in a block.
- saghm 5y agoWhat about iterators? My instinct is that people would run into wanting to use `map` or `filter` or something before they feel the need to spawn threads when using a new language, although that might be my bias coming from a more functional background before learning Rust. The types of closures used as predicates tend to be a lot simpler though, so I guess this may not be what you mean by "confronting" them.
- Animats 5y agoThe recommended solution uses "scoped_threadpool". But "scoped_threadpool" uses "unsafe".[1] They could not actually do this in Rust. They had to cheat. The language has difficulty expressing that items in an array can be accessed in parallel. You can't write that loop in Rust itself without getting the error "error[E0499]: cannot borrow `v` as mutable more than once at a time". And, sure enough, the "unsafe" code once had a hole in it.[2] It's supposedly fixed. If you look at the example with "excessive boilerplate" closely, you can see that it doesn't achieve concurrency at all. It locks the entire array to work on one element of the array, so all but one of the threads are blocked at any time. To illustrate that, I put in a "sleep" and some print statements.[3] You could put a lock on each array element. That would be a legitimate solution that did not require unsafe code. To do this right you need automatic aliasing analysis, which is hard but not impossible. (Been there, done that.) Or a special case for "for foo in bar", which needs to be sure that all elements of "bar" are disjoint". [1] https://github.com/reem/rust-scoped-pool/blob/master/src/lib.rs https://github.com/reem/rust-scoped-pool/blob/master/src/lib... [2] https://www.reddit.com/r/rust/comments/3hyv3i/safe_stable_and_scoped_threadpool https://www.reddit.com/r/rust/comments/3hyv3i/safe_stable_an... [3] https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e6ce779782d5234091cf8d39a4532389 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- jzoch 5y ago> They could not actually do this in Rust. They had to cheat. That isn't true at all. The implementation of scoped_threadpool being in rust is all the evidence you need that they did not cheat. unsafe is a perfectly valid construct in rust that does not subvert its benefits. It just is a reversal of the defaults in other languages (unsafe by default - opt-in to safer constructs like smart_ptr). People freak out too much over this stuff. If you are learning - feel free to use escape hatches from "idiomatic rust" but recognize that you aren't being idiomatic (and therefore may be handicapping your learning!). Its okay to use unsafe despite idiomatic rust trying to avoid it unless necessary (and even then providing safe wrappers over it). Its also okay to use Arc<Mutex> and the like if you need to just recognize there may be a better way.
- steveklabnik 5y agounsafe is not cheating. It is part of the language for a reason, there to be used when appropriate. It is literally impossible to make a useful program without any unsafe code anywhere, because there aren’t Rust CPUs. By this definition, “Rust” does not exist. thread::spawn uses unsafe code. sleep uses unsafe code. Is your own example not in Rust? Are you cheating? (And yes, the spawn version isn’t optimally concurrent. The comment was long enough without getting into that. People struggle to get examples to compile before they worry about things like this. That would be next steps.)