gpt4 book ai didi

function - 克隆具有泛型和函数指针的结构时,如何修复 "the method ` clone` 存在但不满足以下特征边界?

转载 作者:行者123 更新时间:2023-12-03 11:45:06 30 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Deriving a trait results in unexpected compiler error, but the manual implementation works

(2 个回答)


1年前关闭。




我正在编写一个函数,它将在终端中创建一个菜单界面。要使用该函数,需要传递一个 Vec<menu_option>

#[derive(Clone)]
pub struct menu_option<'a, T> {
pub command: &'a str,
pub description: &'a str,
pub action: menu_option_action<'a, T>,
}

#[derive(Clone)]
pub enum menu_option_action<'a, T> {
sub_menu(Vec<menu_option<'a, T>>),
callback(fn(state: T) -> T),
leave,
}
当我设计菜单时,它可以有多个级别,并且可以有 menu_action 的重复。 .即,每一层都有一个“离开”/“返回”:
// In my actual code I construct a vector of `menu_option`s
fn example() {
type StateType = i32;
let mut state: StateType = 88;
let leave_option: menu_option<&mut StateType> = menu_option::<&mut StateType> {
command: "l",
description: "leave",
action: menu_option_action::leave,
};

let a = leave_option.clone();
}
error[E0599]: no method named `clone` found for struct `menu_option<'_, &mut i32>` in the current scope
--> src/lib.rs:24:26
|
2 | pub struct menu_option<'a, T> {
| -----------------------------
| |
| method `clone` not found for this
| doesn't satisfy `menu_option<'_, &mut i32>: std::clone::Clone`
...
24 | let a = leave_option.clone();
| ^^^^^ method not found in `menu_option<'_, &mut i32>`
|
= note: the method `clone` exists but the following trait bounds were not satisfied:
`&mut i32: std::clone::Clone`
which is required by `menu_option<'_, &mut i32>: std::clone::Clone`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `clone`, perhaps you need to implement it:
candidate #1: `std::clone::Clone`
我该如何解决?
我尝试在网上搜索并找不到解决方案,至少,不是一个对我有用的解决方案(我认为是旧的 Rust)
我不知道是什么阻止了克隆,因为 T 的唯一用途是type 是一个函数指针,我认为它是可克隆的。

最佳答案

它在你实现 Clone 时有效你自己在 menu_option & menu_option_action .默认情况下,由于你的 struct/enum 有一个类型参数,#[derive(Clone)] 的宏扩展属性会将您的类型的克隆实现限制为 T: Clone .
在您的情况下,不仅不需要此要求,而且也不太可能得到尊重(例如 &mut T 未实现 Clone )。通过实现 Clone手动,您可以摆脱 T: Clone要求,它的工作原理!
请注意,函数指针实现 Copy这就是为什么 menu_option_action::callback(*f)作品:

type StateType = i32;

pub struct menu_option<'a, T> {
pub command: &'a str,
pub description: &'a str,
pub action: menu_option_action<'a, T>,
}

impl<'a, T> Clone for menu_option<'a, T> {
fn clone(&self) -> Self {
menu_option {
command: self.command.clone(),
description: self.description.clone(),
action: self.action.clone(),
}
}
}

pub enum menu_option_action<'a, T> {
sub_menu(Vec<menu_option<'a, T>>),
callback(fn(T) -> T),
leave,
}

impl<'a, T> Clone for menu_option_action<'a, T> {
fn clone(&self) -> Self {
match self {
menu_option_action::sub_menu(sub) => menu_option_action::sub_menu(sub.to_vec()),
menu_option_action::callback(f) => menu_option_action::callback(*f),
menu_option_action::leave => menu_option_action::leave,
}
}
}

fn main() {
let mut state: StateType = 88;
let leave_option: menu_option<&mut StateType> = menu_option {
command: "l",
description: "leave",
action: menu_option_action::leave,
};

let a = leave_option.clone();
}

关于function - 克隆具有泛型和函数指针的结构时,如何修复 "the method ` clone` 存在但不满足以下特征边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63132174/

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