Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

深入生命周期章节中再借用段落建议 #1020

Open
liuzuwei-go opened this issue Sep 9, 2022 · 0 comments
Open

深入生命周期章节中再借用段落建议 #1020

liuzuwei-go opened this issue Sep 9, 2022 · 0 comments

Comments

@liuzuwei-go
Copy link

对于再借用而言,rr 再借用时不会破坏借用规则,但是你不能在它的生命周期内 再使用 原来的借用 r,来看看对上段代码的分析:

fn main() {
    let mut p = Point { x: 0, y: 0 };
    let r = &mut p;
    // reborrow! 此时对`r`的再借用不会导致跟上面的借用冲突
    let rr: &Point = &*r;

    // 再借用`rr`最后一次使用发生在这里,在它的生命周期中,我们并没有使用原来的借用`r`,因此不会报错
    println!("{:?}", rr);

    // 再借用结束后,才去使用原来的借用`r`
    r.move_to(10, 10);
    println!("{:?}", r);
}

我感觉这里不应该描述为不能再使用,因为只有修改会编译错误,而打印和读值并不会引起编译错误。

fn main() {
    let mut p = Point { x: 0, y: 0 };
    let r = &mut p;
    // reborrow! 此时对`r`的再借用不会导致跟上面的借用冲突
    let rr: &Point = &*r;
   
    // 打印和读值
    println!("{:?}", r);
    println!("{:?}", r.x);

    let y = r.x;
    println!("{:?}", y);

    // 再借用`rr`最后一次使用发生在这里,在它的生命周期中,我们并没有使用原来的借用`r`,因此不会报错
    println!("{:?}", rr);

    // 再借用结束后,才去使用原来的借用`r`
    r.move_to(10, 10);
    println!("{:?}", r);
}

还一点就是能否在 引用与借用 章节提一下再借用,在第二遍读的时候总感觉 同一时刻,你只能拥有要么一个可变引用, 要么任意多个不可变引用 这个借用规则不是很严谨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant