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 (9)

  1. Rust, unfortunately, lets you express lower bounds on lifetimes but not upper bounds. There is no way in Rust to express “this object must be released by that point in the program,” a guarantee which is extremely useful.
    Replies (1)
    1. It kinda does that, by using borrows. If A borrows B, then the upper bound of A's lifetime is the borrow of B. You can control the lifetime of the borrow by creating a new mutable reference to B at a particular point (works regardless of whether the original borrow was mutable or immutable).
      Replies (1)
      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?

              Continue this thread (1 replies) →