为什么Rust有ref关键字?可以
match value.try_thing() {
&Some(ref e) => do_stuff(e),
// ...
}
Run Code Online (Sandbox Code Playgroud)
不能由
match value.try_thing() {
&Some(e) => do_stuff(&e),
// ...
}
Run Code Online (Sandbox Code Playgroud) Rust的书称ref关键词为“传统”。当我想遵循隐式建议避免时ref,如何在以下玩具示例中做到这一点?您也可以在操场上找到代码。
struct OwnBox(i32);
impl OwnBox {
fn ref_mut(&mut self) -> &mut i32 {
match *self {
OwnBox(ref mut i) => i,
}
// This doesn't work. -- Even not, if the signature of the signature of the function is
// adapted to take an explcit lifetime 'a and use it here like `&'a mut i`.
// match *self {
// OwnBox(mut i) => &mut i,
// }
// This doesn't work
// …Run Code Online (Sandbox Code Playgroud)