ZhangHanDong / tao-of-rust-codes

《Rust编程之道》随书源码
https://ruststudy.github.io/tao_of_rust_docs/tao_of_rust/
MIT License
1.18k stars 170 forks source link

第13章 unsafe 块中 可变借用检查代码过期 #296

Open ZhangHanDong opened 4 years ago

ZhangHanDong commented 4 years ago

现在的代码示例:

fn main(){
    unsafe {
        let mut a = "hello";
        let b = &a;
        let c = &mut a;
    }
}

按书中的描述,本来应该报借用检查的错误,但是现在因为NLL的引入,上面代码已经正常编译了。

所以,需要修正。要么更改文本说明,要么修改代码为:

fn main(){
    unsafe {
        let mut a = "hello";
        let b = &a;
        let c = &mut a;
        b;
    }
}

上面代码违反了NLL规则,借用检查照样报错。