[英]Rust: Passing borrowed struct to a borrowed enum?
我正在尝试将借来的结构传递给借来的枚举。
#[derive(Copy, Clone)]
pub struct CustomerData {
// Many fields about customers
}
#[derive(Copy, Clone)]
pub struct EmployeeData {
// Many fields about employees
}
pub enum Person {
Customer(CustomerData),
Employee(EmployeeData)
}
fn do_something_with_customer(customer: &CustomerData) {
let person = &Person::Customer(customer);
// This would work, but this can be a large struct.
// let person = &Person::Customer(customer.clone());
general_method(person);
}
fn do_something_with_employee(employee: &EmployeeData) {
let person = &Person::Employee(employee);
// This would work, but this can be a large struct.
// let person = &Person::Employee(employee.clone());
general_method(person);
}
fn general_method(person: &Person) {
}
fn main() {
let person = Person::Customer(CustomerData { });
match &person {
Person::Customer(data) => {
do_something_with_customer(data);
}
Person::Employee(data) => {
do_something_with_employee(data);
}
}
}
编译给我结果:
error[E0308]: mismatched types
--> src/main.rs:19:36
|
19 | let person = &Person::Customer(customer);
| ^^^^^^^^
| |
| expected struct `CustomerData`, found reference
| help: consider dereferencing the borrow: `*customer`
|
= note: expected type `CustomerData`
found type `&CustomerData`
error[E0308]: mismatched types
--> src/main.rs:28:36
|
28 | let person = &Person::Employee(employee);
| ^^^^^^^^
| |
| expected struct `EmployeeData`, found reference
| help: consider dereferencing the borrow: `*employee`
|
= note: expected type `EmployeeData`
found type `&EmployeeData`
我知道Rust编译器不允许我这样做,但是考虑到我也将借用了我的结构体的枚举,我觉得应该可以。
有这种情况的模式/解决方法吗? 也许使用Rc
类型? 在这种情况下,我不希望用它乱扔我的代码。
use std::rc::Rc;
#[derive(Copy, Clone)]
pub struct CustomerData {
// Many fields about customers
}
#[derive(Copy, Clone)]
pub struct EmployeeData {
// Many fields about employees
}
pub enum Person {
Customer(Rc<CustomerData>),
Employee(Rc<EmployeeData>)
}
fn do_something_with_customer(customer: Rc<CustomerData>) {
let person = &Person::Customer(customer);
// This would work, but this can be a large struct.
// let person = &Person::Customer(customer.clone());
general_method(person);
}
fn do_something_with_employee(employee: Rc<EmployeeData>) {
let person = &Person::Employee(employee);
// This would work, but this can be a large struct.
// let person = &Person::Employee(employee.clone());
general_method(person);
}
fn general_method(person: &Person) {
}
fn main() {
let person = Person::Customer(Rc::new(CustomerData { }));
match &person {
Person::Customer(data) => {
do_something_with_customer(data.clone());
}
Person::Employee(data) => {
do_something_with_employee(data.clone());
}
}
}
您已经错误地识别了问题,并且编译器对它的错误注释不屑一顾。
您按如下方式定义枚举:
pub enum Person {
Customer(CustomerData),
Employee(EmployeeData)
}
但是随后您决定您的枚举成员应该是Person::Customer(&CustomerData)
:
fn do_something_with_customer(customer: &CustomerData) {
let person = &Person::Customer(customer);
参考不是传递性的。 因为&CustomerData
是引用,但这并不意味着整个枚举都将是对真实数据的引用(即&Person::Customer(CustomerData)
)。
有两种解决方法: 显而易见的是查看CustomerData
实现Copy
。 如果是这样,您可以取消引用(并因此隐式复制):
fn do_something_with_customer(customer: &CustomerData) {
let person = Person::Customer(*customer);
(这是编译器建议的,因此我很确定您的类型实现了Copy
)
另一个选项是在类型上#[derive(Clone)]
并调用customer.clone()
。 同样,以额外分配为代价。
如果您确实想要枚举中的引用,则需要将枚举定义更改为:
pub enum Person<'a> {
Customer(&'a CustomerData),
Employee(&'a EmployeeData)
}
处理对象属性现在是引用以及所有相关问题的事实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.