简体   繁体   English

为什么在用于护身符时它会不可取用?

[英]Why does iter borrow mutably when used in a pattern guard?

This code: 这段代码:

fn t(r: &[u8]) {
    match r {
        _ if r.iter().any(|&x| x == b';') => {}
        _ => {}
    }
}

gives me the error: 给我错误:

error[E0301]: cannot mutably borrow in a pattern guard
   |
10 |         _ if r.iter().any(|&x| x == b';') => {}
   |              ^^^^^^^^ borrowed mutably in pattern guard

I understand that I can not borrow mutably in match patterns, but why does the compiler think that r.iter() borrows mutably? 我知道我不能在匹配模式中可靠地借用,但为什么编译器认为r.iter()接受? There is a separate method iter_mut for borrowing mutably. iter_mut有一个单独的方法可供借用。

And how can I check that &[u8] contains b';' 我怎样才能检查&[u8]包含b';' without introducing separate functions? 没有引入单独的功能?

The error message is a bit more nuanced — iter doesn't borrow mutably, but the result of iter is being borrowed mutably . 该错误消息是一个比较微妙的- iter不性情不定地借用,但结果iter正在性情不定地借来的 This is because Iterator::any takes self by mutable reference: 这是因为Iterator::any通过可变引用获取self

fn any<F>(&mut self, f: F) -> bool 
where
    F: FnMut(Self::Item) -> bool, 

Here's a reproduction: 这是一个复制品:

struct X;

impl X {
    fn new() -> X { X } 
    fn predicate(&mut self) -> bool { true }
}

fn main() {
    match () {
        _ if X::new().predicate() => {}
        _ => {}
    }
}

I'd just check if the slice contains the value: 我只是检查切片是否contains值:

fn t(r: &[u8]) {
    match r {
        _ if r.contains(&b';') => {}
        _ => {}
    }
}

Realistically, this is an example of the borrow checker being overly aggressive. 实际上,这是借用检查员过于激进的一个例子。 Mutably borrowing something from "outside" the match guard is a bad idea, but mutably borrowing something created in the match guard should be safe. 从比赛后卫的“外线”中相互借用一些东西是一个坏主意,但是可变地借用比赛后卫中创造的东西应该是安全的。

It's likely that this particular case would work when the borrow checker is rewritten to use Rust's MIR layer. 当借用检查器被重写为使用Rust的MIR层时,这种特殊情况很可能会起作用。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 std::iter::Peekable::peek 可变地借用 self 参数? - Why does std::iter::Peekable::peek mutably borrow the self argument? 无法可变地借用图案保护器[E0301] - Cannot mutably borrow in a pattern guard [E0301] 为什么iter()借用列表是不可变的? - Why does iter() borrow the list as immutable? 为什么在这个例子中我可以可变借用两次? - Why can I mutably borrow twice in this example? 用par_iter()替换iter():不能在`Fn`闭包中捕获的外部变量中可变地借用数据 - Replace iter() with par_iter(): cannot borrow data mutably in a captured outer variable in an `Fn` closure 为什么我不能在两个不同的 map 函数中可变地借用一个变量? - Why can I not mutably borrow a variable in two different map functions? 为什么我不能从枚举中可变地借用一个原语? - Why can't I mutably borrow a primitive from an enum? 从可变借用结构可变借用字段 - mutably borrow fields from a mutably borrowed struct 为什么在短路布尔 AND (&amp;&amp;) 的两侧使用 RefCell:borrow_mut 会导致 BorrowMutError? - Why does RefCell:borrow_mut result in a BorrowMutError when used on both sides of a short-circuiting boolean AND (&&)? 当 `guard` 被删除并运行 `RwLockWriteGuard` 类型的 `Drop` 代码时,可能会在此处使用不可变借用 - Immutable borrow might be used here, when `guard` is dropped and runs the `Drop` code for type `RwLockWriteGuard`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM