相关疑难解决方法(0)

在迭代递归结构时无法获得可变引用:不能一次多次借用可变引用

我试图迭代地导航递归数据结构,以便在某个位置插入元素.根据我的有限理解,这意味着对结构的根进行可变引用,并通过对其跟随者的引用连续替换它:

type Link = Option<Box<Node>>;

struct Node {
    next: Link
}

struct Recursive {
    root: Link
}

impl Recursive {
    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;
        while let Some(ref mut node) = *anchor {
            anchor = &mut node.next;
        }
        anchor
    }
}
Run Code Online (Sandbox Code Playgroud)

(Rust操场链接)

但是,这失败了:

error[E0499]: cannot borrow `anchor.0` as mutable more than once at a time
  --> src/main.rs:14:24
   |
14 |         while let Some(ref mut node) = *anchor {
   |                        ^^^^^^^^^^^^
   |                        | …
Run Code Online (Sandbox Code Playgroud)

mutable rust borrowing

16
推荐指数
3
解决办法
2405
查看次数

从HashMap或Vec返回引用会导致借用超出其所在的范围?

我有一个持久的编译错误,其中Rust抱怨我在尝试可变借用时有一个不可变的借位,但是不可变借用来自另一个范围,而我并没有从中带来任何东西.

我有一些代码检查地图中的值,如果它存在,则返回它,否则它需要以各种方式改变地图.问题是我似乎无法找到让Rust同时执行的方法,即使这两个操作完全分开.

这是一些荒谬的代码,它遵循与我的代码相同的结构并展示了问题:

use std::collections::BTreeMap;

fn do_stuff(map: &mut BTreeMap<i32, i32>, key: i32) -> Option<&i32> {
    // extra scope in vain attempt to contain the borrow
    {
        // borrow immutably
        if let Some(key) = map.get(&key) {
            return Some(key);
        }
    }

    // now I'm DONE with the immutable borrow, but rustc still thinks it's borrowed

    map.insert(0, 0); // borrow mutably, which errors
    None
}
Run Code Online (Sandbox Code Playgroud)

这出错了:

error[E0502]: cannot borrow `*map` as mutable because it is also borrowed as immutable
  --> src/lib.rs:14:5
   |
3 …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

16
推荐指数
1
解决办法
1836
查看次数

即使使用NLL,循环中也会发生双重可变借用错误

假设我有几个结构,如下例所示,在next()方法中我需要使用用户提供的缓冲区来拉下一个事件,但是如果这个事件是注释,并且忽略comments标志设置为true,我需要拉下一个事件:

struct Parser {
    ignore_comments: bool,
}

enum XmlEvent<'buf> {
    Comment(&'buf str),
    Other(&'buf str),
}

impl Parser {
    fn next<'buf>(&mut self, buffer: &'buf mut String) -> XmlEvent<'buf> {
        let result = loop {
            buffer.clear();

            let temp_event = self.parse_outside_tag(buffer);

            match temp_event {
                XmlEvent::Comment(_) if self.ignore_comments => {}
                _ => break temp_event,
            }
        };
        result
    }

    fn parse_outside_tag<'buf>(&mut self, _buffer: &'buf mut String) -> XmlEvent<'buf> {
        unimplemented!()
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,即使#![feature(nll)]启用了以下代码,此代码也会出现双重借用错误:

error[E0499]: cannot borrow `*buffer` as mutable more than …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

9
推荐指数
2
解决办法
489
查看次数

由于循环中可变而无法借用

我有以下代码

pub fn read_packet<'a>(buf: &'a mut [u8]) -> &'a [u8] {
    loop {
        read_exact(buf);

        if let Some(packet) = to_packet(buf) {
            return packet;
        }
    }
}

fn read_exact(_: &mut [u8]) {
    todo!()
}

fn to_packet<'a>(_: &'a [u8]) -> Option<&'a [u8]> {
    todo!()
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

error[E0502]: cannot borrow `*buf` as mutable because it is also borrowed as immutable
 --> src/lib.rs:3:9
  |
1 | pub fn read_packet<'a>(buf: &'a mut [u8]) -> &'a [u8] {
  |                    -- lifetime `'a` defined here
2 …
Run Code Online (Sandbox Code Playgroud)

rust

7
推荐指数
1
解决办法
926
查看次数

标签 统计

rust ×4

borrow-checker ×2

borrowing ×1

mutable ×1