引用自<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,
1
共 4 条评论, 1 页
评论区
写评论对应他的注释是 ”// and doesn't overlap with m0”。体会一下 overlap
对以下内容的回复:
因为后面没有再用 m0。NLL实现后生命周期和变量作用域不完全一致了。 如果后面再用 m0,就会报错了。
对以下内容的回复:
为什么 m0 会认为不用了呢? m0 还在lifetime周期里呀。 对以下内容的回复:
2018 的 NLL,m0 认为已经不用了。