rust - 为什么从方法返回可变引用会阻止调用任何其他方法,即使引用超出范围也是如此?

标签 rust borrow-checker

我想写一个简单的键/值存储,可以存储任何东西。我从围绕 HashMap 的小包装开始:

use std::any::{Any, TypeId};
use std::collections::HashMap;

#[derive(Debug)]
struct Pair<'a> {
    key: &'a str,
    value: Box<Any>,
    data_type: TypeId,
}

impl<'a> Pair<'a> {
    fn new<T>(k: &'a str, v: T) -> Self
    where
        T: Any + 'static,
    {
        Self {
            key: k,
            value: Box::new(v),
            data_type: TypeId::of::<T>(),
        }
    }

    fn update<T>(&mut self, new_value: T)
    where
        T: Any + 'static,
    {
        self.data_type = TypeId::of::<T>();
        self.value = Box::new(new_value);
    }

    fn get<T>(&'a self) -> &'a T
    where
        T: Any + 'static,
    {
        self.value.downcast_ref::<T>().unwrap()
    }

    fn get_mut<T>(&'a mut self) -> &'a mut T
    where
        T: Any + 'static,
    {
        self.value.downcast_mut::<T>().unwrap()
    }
}

#[derive(Debug)]
struct Database<'a> {
    data: HashMap<&'a str, Pair<'a>>,
}

impl<'a> Database<'a> {
    fn new() -> Self {
        Self {
            data: HashMap::new(),
        }
    }

    fn insert(&mut self, data: Pair<'a>) {
        self.data.insert(data.key, data);
    }

    fn find(&self, key: &str) -> &'a Pair {
        self.data.get(key).unwrap()
    }

    fn find_mut(&mut self, key: &str) -> &'a mut Pair {
        self.data.get_mut(key).unwrap()
    }

    fn remove(&mut self, key: &str) {
        self.data.remove(key);
    }
}

#[derive(Debug)]
struct Position {
    x: f32,
    y: f32,
}

fn main() {
    let mut db = Database::new();

    // add data
    {
        let pair1 = Pair::new("testkey", "Awesome string...".to_owned());
        let pair2 = Pair::new("position", Position { x: 0.0, y: 0.0 });

        db.insert(pair1);
        db.insert(pair2);
    }

    // change data
    {
        let pair = db.find_mut("position");
        pair.get_mut::<Position>().x = 50.0;
    } // <--- end of &mut Pair

    // read data
    let pos = db.find("position");

    println!("{:?}", pos);
}
error[E0502]: cannot borrow `db` as immutable because it is also borrowed as mutable
   --> src/main.rs:101:15
    |
96  |         let pair = db.find_mut("position");
    |                    -- mutable borrow occurs here
...
101 |     let pos = db.find("position");
    |               ^^
    |               |
    |               immutable borrow occurs here
    |               mutable borrow later used here

我不明白这里的借用检查器。我确定了所有内容的范围,因此 db.find("position") 不存在 pair。为什么它不起作用?如果我对文档的理解正确,那就是在嵌套范围内使用可变变量。

我写了一个更简单的例子,我来自:

fn main() {
    let mut x = 5;

    {
        let y = &mut x;
        *y = 10;
    }

    println!("{}", x);
}

这按预期工作。我真的被借用检查器困住了。

最佳答案

长话短说

fn get<T>(&self) -> &T
fn get_mut<T>(&mut self) -> &mut T

fn find(&self) -> &Pair<'a>
fn find_mut(&mut self) -> &mut Pair<'a>

创建 MCVE是成为高效程序员的一项至关重要的技能。我们在 the tag wiki 上对特定于 Rust 的技术进行了分类.这是您的代码之一:

#[derive(Debug, Default)]
struct Pair<'a>(&'a str);

#[derive(Debug, Default)]
struct Database<'a> {
    data: &'a str,
}

impl<'a> Database<'a> {
    fn find(&self) -> &'a Pair {
        unimplemented!()
    }

    fn find_mut(&mut self) -> &'a mut Pair {
        unimplemented!()
    }
}

fn main() {
    let mut db = Database::default();
    {
        db.find_mut();
    }
    db.find();
}

问题的发生是因为你在没有正确的情况下散布了生命周期。具体来说:

fn find(&self) -> &'a Pair
fn find_mut(&mut self) -> &'a mut Pair

这些方法表示它们将返回一个的引用,该引用的持续时间与您插入数据库的数据一样长.这不可能是真的,因为您插入的数据是 &'static str

你实际上想要:

fn find(&self) -> &Pair<'a>
fn find_mut(&mut self) -> &mut Pair<'a>

#![deny(rust_2018_idioms)] 添加到你的 crate 有助于捕获这些错误消息,尽管错误消息还不完美:

error: hidden lifetime parameters in types are deprecated
  --> src/main.rs:12:27
   |
12 |     fn find(&self) -> &'a Pair {
   |                           ^^^^- help: indicate the anonymous lifetime: `<'_>`

error: hidden lifetime parameters in types are deprecated
  --> src/main.rs:16:39
   |
16 |     fn find_mut(&mut self) -> &'a mut Pair {
   |                                       ^^^^- help: indicate the anonymous lifetime: `<'_>`

如果我们再扩展回完整的原始问题,我们会发现问题仍然没有消失。由于我们刚刚遇到了 Pair 内部的生命周期问题,让我们看看是否还有其他相关问题:

fn get<T>(&'a self) -> &'a T
fn get_mut<T>(&'a mut self) -> &'a mut T

这表明 self 将与 self.key 一样长,这在 99% 的情况下都不是您想要的。删除这些明确的生命周期以允许正常的生命周期省略发生:

fn get<T>(&self) -> &T
fn get_mut<T>(&mut self) -> &mut T

另见:

关于rust - 为什么从方法返回可变引用会阻止调用任何其他方法,即使引用超出范围也是如此?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54887383/

相关文章:

mysql - 我无法在 MariaDB 上使用 Diesel 进行绑定(bind)

optimization - 我可以在 Rust 中按值有效地返回对象吗?

utf-8 - 如何在Rust中逐行读取非UTF8文件

rust - 借用循环内使用的可变成员

rust - 为什么允许在 &mut self 中借用结构成员,但不允许借用 self 到不可变方法?

rust - 如何在 Vec 上更新或插入?

pointers - 取消引用指向 String 的原始指针和指向 i32 的原始指针之间有什么区别?

rust - 如何为字段编写具有生命周期约束的函数?

reference - 为什么我可以返回对局部文字的引用而不是变量?

Rust 泛型参数和编译时间 if