< 返回版块

koodersun 发表于 2019-05-15 14:47

Tags:reference, borrow

引用自<OReilly.Programming Rust> | Chapter 5 | Sharing Versus Mutation

You can reborrow from a mutable reference:
let mut v = (136, 139);
let m = &mut v;
let m0 = &mut m.0; // ok: reborrowing mutable from mutable
*m0 = 137;
let r1 = &m.1; // ok: reborrowing shared from mutable,
// and doesn't overlap with m0
v.1; // error: access through other paths still forbidden

有个疑问,既然Sharable 和 Mutable 不能同时存在,为何这里时合法的呢?

let r1 = &m.1; // ok: reborrowing shared from mutable,

评论区

写评论
作者 koodersun 2019-05-15 16:31

为什么 m0 会认为不用了呢? m0 还在lifetime周期里呀。 对以下内容的回复:

Ryan-Git 2019-05-15 16:08

2018 的 NLL,m0 认为已经不用了。

1 共 2 条评论, 1 页