gpt4 book ai didi

enums - Rust:将借用的结构传递给借用的枚举?

转载 作者:行者123 更新时间:2023-11-29 08:26:08 26 4
gpt4 key购买 nike

我正在尝试将借用的结构传递给借用的枚举。

#[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)
}

并处理对象属性现在是一个引用这一事实,以及所有相关的问题。

关于enums - Rust:将借用的结构传递给借用的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com