An unofficial, read-only Hacker News reader. Join the beta →

2035 came early (opens external site)

1035 points by setheron 4 days ago | fzakaria.com | 382 comments on HN

View full thread · View on Hacker News

Comment thread (6)

  1. I fail to see the difference between the lifetime of the variable being longer than the thread and the lifetime of the thread being shorter than the variable. I also fail to see how flipping it around would help eliminate std::thread::scope, which exists because of Rc cycles. Could you please show an example of what kind of code would be disallowed if upper bounds existed that is not possible today?
    Replies (1)
    1. Here is your example:

        use std::thread;
        fn main() {
          let v = vec![1, 2, 3];
          let handle = thread::spawn(|| {
            println!("{:?}", v); // borrows `v`
          });
          handle.join().unwrap();
        }
      
      If lifetimes could have upper bounds then this would work.
      Replies (1)
      1. Define "would work". As far as I know, this already existed in pre-1.0 Rust, it compiled, it worked, it prevented borrowing variables with shorter lifetime than the thread, and it only fell apart if you did something very questionable with cyclical Rc's.

        Would upper bounds prevent falling apart if you did something very questionable with cyclical Rc's? If so, how so? What kind of code would compiler REJECT that it wouldn't today?

        Replies (1)
        1. “Would work” = would compile and run as intuitively expected.

          I don’t understand the second paragraph. “Questionable” is vague, lots of things are questionable. The last sentence I don’t understand.

          I’m illustrating a limitation of the compiler and of Rust as it exists today, not some pre-1.0 version, and I’m not comparing it to some complete alternative compiler without that limitation. The sample code above is an illustration of the limitation only.

          Replies (1)
          1. I think your parent is thinking of the thread::scoped API, which was removed before Rust 1.0, and is back now. And it illustrates a technique to do what it is that you want, even if you can’t literally do what you want, but there’s sort of a parallel to defer vs RAII, in that it doesn’t really let you fully escape the parent scope, but does give you what you want in certain circumstances.
            Replies (1)
            1. Right, if your use case works with thread::scope (as I mentioned) you can get it working.

              The thread::scope is kind of a workaround that may or may not work for your use case and I trust people are able to figure out that I’m illustrating a limitation, and not asking “how can I get this code to work”.

              If lifetimes could be bounded from above, then we could say that references which the closure borrows are bounded above by the scope containing the thread. We can’t do this, so we use thread::scope instead, which introduces a second closure just to work around the limitation.