相关疑难解决方法(0)

什么是非词汇生命?

Rust有一个与非词汇生命周期相关的RFC,已被批准在该语言中实现了很长时间.最近,Rust对此功能的支持有了很大改进,并且被认为是完整的.

我的问题是:非词汇生命究竟是什么?

lifetime rust lifetime-scoping

63
推荐指数
1
解决办法
6149
查看次数

有没有办法在超出范围之前释放绑定?

我正在尝试使用正则表达式解析文件:

extern crate regex; // 1.0.1

use regex::Regex;

fn example(
    section_header_pattern: Regex,
    section_name: &str,
    mut line: String,
    mut is_in_right_section: bool,
) {
    loop {
        if let Some(m) = section_header_pattern
            .captures(&line)
            .and_then(|c| c.get(1))
        {
            is_in_right_section = m.as_str().eq(section_name);
            line.clear();
            continue;
        }
    }
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

......但是编译器会抱怨,因为RegExcaptures()方法有哪些承受了本场比赛的一生借:

error[E0502]: cannot borrow `line` as mutable because it is also borrowed as immutable
  --> src/main.rs:17:13
   |
13 |             .captures(&line)
   |                        ---- immutable borrow occurs here
...
17 |             line.clear();
   |             ^^^^ …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

10
推荐指数
1
解决办法
212
查看次数

`if`条件仍在借用

我刚刚遇到Rust的一些行为(1.12)我无法解释.我有一个结构,实现延迟加载与a RefCell<Option<i32>>和一个访问数据的函数:

struct Foo {
    data: RefCell<Option<i32>>
}

impl Foo {
    fn get_data(&self) -> i32 {
        if self.data.borrow().is_none() { // <--- (a)
            let d = 1337;
            self.data.borrow_mut() = Some(d); // <--- (b)
            d
        } else {
            self.data.borrow().unwrap()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这会编译但会产生运行时错误:RefCell抱怨在尝试borrow_mut上线(b)时借用已经处于活动状态.但是,如果我将if语句更改为以下内容,则不会发生此问题:

let is_none = self.data.borrow().is_none();
if is_none {
Run Code Online (Sandbox Code Playgroud)

问题:为什么第(a)行的if条件中的借位在if语句的主体内仍然有效?不应该打电话is_none()导致借款结束,因为我只是坚持一个bool事后,而不是借来的价值?

rust

5
推荐指数
1
解决办法
160
查看次数

如果向量为空,如何在插入值时避免向量的多个可变借位?

这个github讨论中,你会发现这个代码引起了借用检查器的愤怒:

fn main() {
    let mut vec = vec!();

    match vec.first() {
        None => vec.push(5),
        Some(v) => unreachable!(),
    }
}
Run Code Online (Sandbox Code Playgroud)

我理解为什么在不可变借用突出的情况下进行突变是有问题的.我假设一个解决方案是明确地只有一个借用(一个可变的),但它仍然导致我有两个借款,一个不可变的借款,然后是一个可变的借款:

fn main() {
    let mut vec: Vec<i32> = vec!();

    let r_vec: &mut Vec<i32> = &mut vec;

    match r_vec.first() {
       None => r_vec.push(5),
       Some(v) => unreachable!(),
   }   
} 
Run Code Online (Sandbox Code Playgroud)

编译器仍然不满意:

error[E0502]: cannot borrow `*r_vec` as mutable because it is also borrowed as immutable
 --> testrust.rs:7:17
  |
6 |     match r_vec.first() {
  |           ----- immutable borrow occurs here
7 | …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

4
推荐指数
1
解决办法
627
查看次数

为什么对于自定义类型而不是 Box,我会得到“无法移出`item`,因为它是借来的”?

代码:

use std::collections::HashSet;
use std::{mem, ptr, fmt};
use std::ops::Deref;

enum Unsafety {
    Normal
}
enum ImplPolarity { Positive }
struct TraitRef;
struct Ty;
struct ImplItem;

enum ItemKind {
    Impl(Unsafety,
             ImplPolarity,
             Option<TraitRef>, // (optional) trait this impl implements
         Box<Ty>, // self
    ),
}

struct Item {
    node: ItemKind,
}

pub struct P<T: ?Sized> {
    ptr: Box<T>
}

impl<T: 'static> P<T> {
    pub fn unwrap(self) -> T {
        *self.ptr
    }
}

impl<T: ?Sized> Deref for P<T> {
    type Target = T;

    fn …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
952
查看次数

如果不能,"不能一次多次借用可变的东西"

我正在编写一个程序来计算单词出现的频率.这是我的代码的一部分.

// hm is a HashMap<&str, u32>
if let Some(val) = hm.get_mut(tt) {
    *val += 1u32;
} else {
    hm.insert(tt.clone(), 1u32);
}
Run Code Online (Sandbox Code Playgroud)

我得到了......

error: cannot borrow `hm` as mutable more than once at a time [E0499]
      hm.insert(tt.clone(), 1u32);
      ^~
note: first mutable borrow occurs here
            if let Some(val) = hm.get_mut(tt) {
                            ^~
note: first borrow ends here
            }
            ^
help: run `rustc --explain E0499` to see a detailed explanation
Run Code Online (Sandbox Code Playgroud)

我可以通过hm.insert()移出else范围来绕过这个但是它是一种"非程序化"的方式......我尝试使用match但是同样的错误(很明显)会发生.

我怎样才能解决这个问题?

rust

0
推荐指数
1
解决办法
781
查看次数

标签 统计

rust ×6

borrow-checker ×2

lifetime ×1

lifetime-scoping ×1