6 karma · joined May 15, 2015 (11y ago) · 5 submissions
comment 11y ago · link · parent
comment 11y ago · link · parent
> 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
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.