锈匹配和借阅检查器 [英] Rust matching and borrow checker

查看:93
本文介绍了锈匹配和借阅检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在我的Rust程序中绊脚石,这总是使我与借位检查器不合.考虑以下玩具示例:

I keep stumbling on a pattern in my Rust programs that always puts me at odds with the borrow-checker. Consider the following toy example:

use std::sync::{Arc,RwLock};

pub struct Test {
    thing: i32,
}

pub struct Test2 {
    pub test: Arc<RwLock<Test>>,
    pub those: i32,
}

impl Test {
    pub fn foo(&self) -> Option<i32> {
        Some(3)
    }
}

impl Test2 {
    pub fn bar(&mut self) {
        let mut test_writer = self.test.write().unwrap();

        match test_writer.foo() {
            Some(thing) => {
                self.add(thing);
            },
            None => {}
        }
    }

    pub fn add(&mut self, addme: i32) {
        self.those += addme;
    }
}

之所以无法编译,是因为Some臂中的add函数试图以可变方式借用self,为了打开读写锁,该对象已经在match语句上方以不变的方式借用了.

This doesn't compile because the add function in the Some arm tries to borrow self mutably, which was already borrowed immutably just above the match statement in order to open the read-write lock.

我在Rust中几次遇到了这种模式,主要是在使用RwLock时.我还找到了一种解决方法,即在match语句之前引入布尔值,然后在Some分支中更改布尔值,然后最终在match语句之后对该布尔值引入测试以执行任何操作我想在Some臂上做吗?

I've encountered this pattern a few times in Rust, mainly when using RwLock. I've also found a workaround, namely by introducing a boolean before the match statement and then changing the value of the boolean in the Some arm and then finally introducing a test on this boolean after the match statement to do whatever it is I wanted to do in the Some arm.

在我看来,这并不是解决问题的方法,我认为在Rust中有一种更惯用的方法可以做到这一点-或以完全不同的方式解决问题-但我找不到它.如果我没记错的话,问题就与词汇借用有关,因此self不能在match语句的范围内可变地借用.

It just seems to me that that's not the way to go about it, I assume there's a more idiomatic way to do this in Rust - or solve the problem in an entirely different way - but I can't find it. If I'm not mistaken the problem has to do with lexical borrowing so self cannot be mutably borrowed within the arms of the match statement.

有没有惯用的Rust方法来解决这类问题?

Is there an idiomatic Rust way to solve this sort of problem?

推荐答案

直接使用字段those,例如,使用自定义类型:

Use directly the field those, for example with custom type:

use std::sync::{Arc,RwLock};

pub struct Those(i32);

impl Those {
    fn get(&self) -> i32 {
        self.0
    }

    fn add(&mut self, n: i32) {
        self.0 += n;
    }
}

pub struct Test {
    thing: Those,
}

pub struct Test2 {
    pub test: Arc<RwLock<Test>>,
    pub those: Those,
}

impl Test {
    pub fn foo(&self) -> Option<Those> {
        Some(Those(3))
    }
}

impl Test2 {
    pub fn bar(&mut self) {
        let mut test_writer = self.test.write().unwrap();

        match test_writer.foo() {
            Some(thing) => {
                // call a method add directly on your type to get around the borrow checker
                self.those.add(thing.get());
            },
            None => {}
        }
    }
}

这篇关于锈匹配和借阅检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆