匹配 Rc<RefCell<T>>
Matching with Rc<RefCell<T>>
考虑以下代码 - 它可以编译并运行:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::{Something, Nothing};
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped = Rc::new(RefCell::new(Foo::Nothing));
//....
match *wrapped.borrow() {
Something(x) => println!("{}", x),
Nothing => println!("Nothing"),
};
}
现在我想匹配两个包装值而不是一个:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::{Something, Nothing};
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped1 = Rc::new(RefCell::new(Foo::Nothing));
let wrapped2 = Rc::new(RefCell::new(Foo::Nothing));
//....
match (*wrapped1.borrow(), *wrapped2.borrow()) {
(Something(x), Something(y)) => println!("{}, {}", x, y),
_ => println!("Nothing"),
};
}
现在这将给出编译错误:
error[E0507]: cannot move out of dereference of `std::cell::Ref<'_, Foo>`
--> src\main.rs:16:12
|
16 | match (*wrapped1.borrow(), *wrapped2.borrow()) {
| ^^^^^^^^^^^^^^^^^^ move occurs because value has type `Foo`, which does not implement the `Copy` trait
error[E0507]: cannot move out of dereference of `std::cell::Ref<'_, Foo>`
--> src\main.rs:16:32
|
16 | match (*wrapped1.borrow(), *wrapped2.borrow()) {
| ^^^^^^^^^^^^^^^^^^ move occurs because value has type `Foo`, which does not implement the `Copy` trait
我不太明白这两个例子在语义上的根本区别。为什么会发生这种情况,使第二个片段起作用的正确方法是什么?
match (*wrapped1.borrow(), *wrapped2.borrow()) {
您当场在这里创建了一个元组。并且值被移动到新创建的元组中。这虽然有效:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::Something;
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped1 = Rc::new(RefCell::new(Foo::Nothing));
let wrapped2 = Rc::new(RefCell::new(Foo::Nothing));
match (&*wrapped1.borrow(), &*wrapped2.borrow()) {
(Something(x), Something(y)) => println!("{}, {}", x, y),
_ => println!("Nothing"),
};
}
考虑以下代码 - 它可以编译并运行:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::{Something, Nothing};
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped = Rc::new(RefCell::new(Foo::Nothing));
//....
match *wrapped.borrow() {
Something(x) => println!("{}", x),
Nothing => println!("Nothing"),
};
}
现在我想匹配两个包装值而不是一个:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::{Something, Nothing};
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped1 = Rc::new(RefCell::new(Foo::Nothing));
let wrapped2 = Rc::new(RefCell::new(Foo::Nothing));
//....
match (*wrapped1.borrow(), *wrapped2.borrow()) {
(Something(x), Something(y)) => println!("{}, {}", x, y),
_ => println!("Nothing"),
};
}
现在这将给出编译错误:
error[E0507]: cannot move out of dereference of `std::cell::Ref<'_, Foo>`
--> src\main.rs:16:12
|
16 | match (*wrapped1.borrow(), *wrapped2.borrow()) {
| ^^^^^^^^^^^^^^^^^^ move occurs because value has type `Foo`, which does not implement the `Copy` trait
error[E0507]: cannot move out of dereference of `std::cell::Ref<'_, Foo>`
--> src\main.rs:16:32
|
16 | match (*wrapped1.borrow(), *wrapped2.borrow()) {
| ^^^^^^^^^^^^^^^^^^ move occurs because value has type `Foo`, which does not implement the `Copy` trait
我不太明白这两个例子在语义上的根本区别。为什么会发生这种情况,使第二个片段起作用的正确方法是什么?
match (*wrapped1.borrow(), *wrapped2.borrow()) {
您当场在这里创建了一个元组。并且值被移动到新创建的元组中。这虽然有效:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::Something;
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped1 = Rc::new(RefCell::new(Foo::Nothing));
let wrapped2 = Rc::new(RefCell::new(Foo::Nothing));
match (&*wrapped1.borrow(), &*wrapped2.borrow()) {
(Something(x), Something(y)) => println!("{}, {}", x, y),
_ => println!("Nothing"),
};
}