我正在尝试使用正则表达式解析文件:
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)
......但是编译器会抱怨,因为RegEx
的captures()
方法有哪些承受了本场比赛的一生借:
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的一些行为(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
事后,而不是借来的价值?
在这个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) 代码:
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) 我正在编写一个程序来计算单词出现的频率.这是我的代码的一部分.
// 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
但是同样的错误(很明显)会发生.
我怎样才能解决这个问题?