gpt4 book ai didi

rust - Change selector in match when selector is a mutable reference

转载 作者:行者123 更新时间:2023-11-29 07:59:39 27 4
gpt4 key购买 nike

我想根据 Iterator::next 中当前枚举变体的某些属性更改枚举变体。我有两次尝试,都没有编译:

enum Test {
A(Vec<usize>),
B,
}

impl<'a> Iterator for Test {
type Item = usize;

fn next(&mut self) -> Option<Self::Item> {
// attempt 1
if let Test::A(ref a) = *self {
if a.len() == 0 {
*self = Test::B; // doesn't work because a is borrowed
};
}

// attempt 2
*self = match *self {
Test::A(ref a) if a.len() == 0 => Test::B,
_ => *self, // cannot move out of borrowed context
};
None
}
}

fn main() {}

如果我不使用选择器中的引用,我的第二次尝试确实有效:

let mut a = Test::A(vec![]);
a = match a {
Test::A(ref a) if a.len() == 0 => Test::B,
_ => a,
};

这个问题与Is there a way to use match() in rust when modifying the selector?有关, 但那里提出的解决方案不是通用的:只有在两个分支中执行相同的功能时它才有效。

实现我的目标的 Rustacean 方法是什么?

最佳答案

由于将条件放在 if let/match block 中时可读性不是很好,我会使用辅助函数来测试它:

impl Test {
fn is_empty_a(&self) -> bool {
if let Test::A(ref a) = *self {
a.len() == 0
} else {
false
}
}
}

然后不应该有任何借贷问题:

impl<'a> Iterator for Test {
type Item = usize;

fn next(&mut self) -> Option<Self::Item> {
if self.is_empty_a() {
*self = Test::B;
}
None
}
}

关于rust - Change selector in match when selector is a mutable reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44899992/

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