What it does
the borrow checker checks when a reference got out of its scope, but not for raw pointers, where thing might get weird.
for example:
fn local_ptr() -> *const i32 {
let temp: i32 = 123;
&temp as *const i32
}
fn main() {
let p = local_ptr();
unsafe {
println!("{}", *p);
}
}
The above code prints 0 (Because temp was allocated in the stack???), run it in playground.
I don't think there was anyway to help people avoid such code, if there is, please let me know.
Advantage
- Help reducing undefined behaviors
Drawbacks
Might be hard to cover all the cases while not causing false positive results.
Example
fn local_ptr() -> *const i32 {
let temp: i32 = 123;
&temp as *const i32
}
Could be written as:
fn local_ptr() -> *const i32 {
static temp: i32 = 123;
&temp as *const i32
}
What it does
the borrow checker checks when a reference got out of its scope, but not for raw pointers, where thing might get weird.
for example:
The above code prints 0 (Because
tempwas allocated in the stack???), run it in playground.I don't think there was anyway to help people avoid such code, if there is, please let me know.
Advantage
Drawbacks
Might be hard to cover all the cases while not causing false positive results.
Example
Could be written as: