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

标签 rust

我想根据 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/

相关文章:

rust - 有没有办法以有意义的方式声明变量不可变?

rust - 如何基于 `&[Box<dyn CustomTrait>]` 创建向量?

rust - 有没有办法在 Rust 库中包含二进制文件或文本文件?

testing - 为集成测试和基准测试共享实用程序函数的惯用方法是什么?

rust - 将数据附加到 Actix-web 中的代理响应

csv - 如何匹配 nom 中的 CSV 样式带引号的字符串?

rust - 可变地传递一个不变地借用的变量

rust - 为什么 Drop 使用 &mut self 而不是 self?

rust - 操作返回 Box<Future> 时的生命周期编译器错误

rust - 保存可变引用以备后用,即使在使用别名时也是如此