Better HN
topnewbestaskshowjobs
Log inSign up

borrowwhatnow

6 karma · joined May 15, 2015 (11y ago) · 5 submissions

Submissions

comment 11y ago · link · parent

True, but pointers don't have destructors in C++ or Rust, I think (and in C, there are no destructors at all, obvs).

comment 11y ago · link · parent

I think I disagree with the "your code would be nutty in C" comment. Since C requires all variables to be declared at the top of the scope, and the order of declaration doesn't matter for purposes of allocation, there's no reason to worry about the ordering in this case.

comment 11y ago · link · parent

Well, that whole "ref" thing just shows my ignorance of Rust. Thanks for the information.

> That would leave 'ref' dangling, if even for a moment, like here. That'd be bad.

I'm not sure I agree that it would be bad (except maybe from a purity standpoint). Ref is unreachable during the time it is "pointing" to the out-of-scope "val", so nothing can actually reach the out-of-scope value through ref.

comment 11y ago · link · parent

> I'll assume you meant 'ref = &x' in your example.

I did, thanks.

comment 11y ago · link · parent

Congrats! I've been excited about Rust for a while now, and this gives me a good reason to look into it a bit more. A quick question, though, if it's appropriate in this context:

It seems to me that the borrow checker disallows behavior that I (perhaps naively) wouldn't be concerned about. For example, if I have this C snippet:

  { // Safe C
    int *ptr = NULL;
    int val = 42;
    ptr = &val;
  }
Everything is hunky-dory. Translating this to Rust results in this (I think):

  { // Malformed Rust
    let ref: &i32;
    let val = 42;
    ref = &val;
  }
Now the borrow checker tells me that "val" doesn't live long enough, because the reference comes to life before the value. It seems like this should be safe behavior, because the reference isn't bound to the value until after the value comes to life.

Can anyone shed some light on this for me? I'm all for safety, but this in particular seems like something that I should be allowed to do.