An unofficial, read-only Hacker News reader.

2035 came early (opens external site)

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

View full thread · View on Hacker News

Comment thread (7)

  1. In this case you’re really setting a lower bound on the borrow of B, not an upper bound on the lifetime of A.

    I assure you this is not pedantry or a nitpick. The classic case here is if you want to borrow a local variable inside a thread… you need to ensure that the thread lifetime is bounded from above by the lifetime of the borrowed variable. This is why we have weird workarounds like std::thread::scope. If we could just express an upper bound on lifetime, it wouldn’t be necessary.

    Replies (1)
    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.

              Continue this thread (1 replies) →