News for hackers. Stuff that matters.
Re:Things you can’t do in Rust (and what to do instead) by tene on Monday May 17, 2021 @10:31PM (#27190687)
You mention this, but I want to clarify that you only need reference counting due to using only std, not due to limitations in the language.

Scoped threads are available in crossbeam, and they're pretty great, as sometimes it's convenient to borrow something from the stack.

Here's how I'd write that with scoped threads:

    let my_string = Mutex::new(String::new());
    crossbeam::scope(|s| {
        for _ in 0..10 {
            s.spawn(|_| {
                let mut guard = my_string.lock().unwrap();
                guard.push_str("some characters");
            });
        }
    }).unwrap();
Re:Things you can’t do in Rust (and what to do instead) by oconnor663 on Wednesday May 19, 2021 @09:44PM (#27216463)
Yeah Crossbeam is awesome. And the pre-1.0 story about a similar interface that existed in std, until it was found to be unsound, is totally fascinating: https://github.com/rust-lang/rust/issues/24292. (Prior to that, std::mem::forget was marked unsafe!)

Another detail that's worth clarifying for folks reading along: The API in this example will automatically join all threads at the end of the crossbeam::scope. (That is indeed what makes it safe.) So if your goal is to fire off a background thread that will outlive the current function, you usually need to go back to std::thread::spawn and live with its 'static requirement.